Problem p00471 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

template <typename T> void fill_all(T &ar, const T &v) { ar = v; }
template <typename T, size_t N, typename U>
void fill_all(T (&ar)[N], const U &v) {
  for (auto &i : ar) {
    fill_all(i, v);
  }
}

int w, h;
int map[10][10];
bool passed[10][10];
int gx, gy;
int count_max;
int func(int x, int y, int count) {
  if (count > 0) {
#ifdef DEBUG
// std::cout << "IN:"<<x<<' '<<y<<' '<<count<<'\\n';
#endif
    if (x == gx && y == gy) {
      return 0;
    }
    if (count == count_max) {
      auto DP = [&]() {}; //{std::cout <<x<<' '<<y<<'\\n'; };
      for (int nx = 0; nx < w; ++nx) {
        if (map[nx][y] == 2) {
          DP();
          return 1;
        }
      }
      for (int ny = 0; ny < h; ++ny) {
        if (map[x][ny] == 2) {
          DP();
          return 1;
        }
      }
      return 0;
    }
    passed[x][y] = true;
  }
  int res = 0;
  for (int nx = x - 1; nx >= 0; --nx) {
    if (!passed[nx][y] && map[nx][y] == 1) {
      res += func(nx, y, count + 1);
      break;
    }
  }
  for (int nx = x + 1; nx < w; ++nx) {
    if (!passed[nx][y] && map[nx][y] == 1) {
      res += func(nx, y, count + 1);
      break;
    }
  }
  for (int ny = y - 1; ny >= 0; --ny) {
    if (!passed[x][ny] && map[x][ny] == 1) {
      res += func(x, ny, count + 1);
      break;
    }
  }
  for (int ny = y + 1; ny < h; ++ny) {
    if (!passed[x][ny] && map[x][ny] == 1) {
      res += func(x, ny, count + 1);
      break;
    }
  }
  passed[x][y] = false;
  return res;
}

int main() {
  for (;;) {
    std::cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    count_max = 0;
    for (int y = 0; y < h; ++y)
      for (int x = 0; x < w; ++x) {
        std::cin >> map[x][y];
        if (map[x][y] == 1) {
          ++count_max;
        }
        if (map[x][y] == 2) {
          gx = x;
          gy = y;
        }
      }
    // std::cout << "a:"<<gx<<' '<<gy<<' '<<count_max<<'\\n';

    int res = 0;
    std::cout << func(gx, gy, 0) << std::endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

template <typename T> void fill_all(T &ar, const T &v) { ar = v; }
template <typename T, size_t N, typename U>
void fill_all(T (&ar)[N], const U &v) {
  for (auto &i : ar) {
    fill_all(i, v);
  }
}

int w, h;
int map[10][10];
bool passed[10][10];
int gx, gy;
int count_max;
int func(int x, int y, int count) {
  std::cout << "IN:" << x << ' ' << y << ' ' << count << '\\n';
  if (count == count_max) {
    if (x == gx || y == gy) {
      return 1;
    }
    return 0;
  }
  passed[x][y] = true;
  int res = 0;
  for (int nx = x - 1; nx >= 0; --nx) {
    if (!passed[nx][y] && map[nx][y] == 1) {
      res += func(nx, y, count + 1);
      break;
    }
  }
  for (int nx = x + 1; nx < w; ++nx) {
    if (!passed[nx][y] && map[nx][y] == 1) {
      res += func(nx, y, count + 1);
      break;
    }
  }
  for (int ny = y - 1; ny >= 0; --ny) {
    if (!passed[x][ny] && map[x][ny] == 1) {
      res += func(x, ny, count + 1);
      break;
    }
  }
  for (int ny = y + 1; ny < h; ++ny) {
    if (!passed[x][ny] && map[x][ny] == 1) {
      res += func(x, ny, count + 1);
      break;
    }
  }
  passed[x][y] = false;
  return res;
}

int main() {
  for (;;) {
    std::cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    count_max = 0;
    for (int y = 0; y < h; ++y)
      for (int x = 0; x < w; ++x) {
        std::cin >> map[x][y];
        if (map[x][y] == 1) {
          ++count_max;
        }
        if (map[x][y] == 2) {
          gx = x;
          gy = y;
        }
      }
    std::cout << "a:" << gx << ' ' << gy << ' ' << count_max << '\\n';

    int res = 0;
    std::cout << func(gx, gy, 0) << std::endl;
  }
}

#if 0
int w,h;
const int goal = 23;
const int empty = 24;

int graph[25][4][25];//????????????
int map[10][10];

const int nposmax = 6;
const int bitmax = 1<<21;
//bit????????????
int dp[nposmax][bitmax];
int func(int now, int bit){
	if(now==empty){return 0;}
	if(now==goal){
#ifdef DEBUG
	std::cout<<"OUTg0:" << now <<' '<<bit<<':'<<((now==goal&&bit==0)?1:0)<<std::endl;
#endif
		return ((now==goal&&bit==0)?1:0);
	}
	if(bit==0){
		for(auto& g:graph[now]){
			if(g[0] == goal){
				return 1;
			}
		}
		return 0;
	}
	
	if(bit<bitmax&&now<nposmax)if(dp[now][bit]>=0){return dp[now][bit];}
	int res = 0;
	
	for(int graph_num =0;graph_num<4;++graph_num){
		for(auto iter=graph[now][graph_num];*iter!=empty;++iter){
			if(bit&(1<<*iter)){
				res += func(*iter,bit&(~(1<<*iter)));
			}
			else{
				break;
			}
		}
	}
	
	
	if(bit<bitmax&&now<nposmax)return dp[now][bit]=res;
	return res;
}
int main()
{
	for(;;){
	fill_all(dp,-1);
	fill_all(map,empty);
	fill_all(graph,empty);
	std::cin>>w>>h;if(w==0&&h==0){break;}
	int count = 0;
	for(auto y=0;y<h;++y)for(auto x=0;x<w;++x){
		std::cin >> map[y][x];
		if(map[y][x]==0){
			map[y][x]=empty;
			continue;
		}
		else if(map[y][x]==1){
			map[y][x]=count++;
		}
		else if(map[y][x]==2){
			map[y][x]=goal;
		}
			auto pushback=[](int*g, int v){
				while(*g!=empty){++g;}*g=v;
			};
			auto iter = graph[map[y][x]][0];
			for(int ox=x-1;ox>=0;--ox){
				if(empty>map[y][ox]){
#ifdef DEBUG
	std::cout <<"PASSx "<<x<<' '<<y<<"<->"<<ox<<' '<<y<<' '<<map[y][ox] <<"\\n";
#endif
					*(iter++) = map[y][ox];
					pushback(graph[map[y][ox]][2],map[y][x]);
#ifdef DEBUG
	std::cout <<"graph "<<*(iter-1) <<' '<<map[y][x]<<"\\n";
#endif
				}
			}
			iter = graph[map[y][x]][1];
			for(int oy=y-1;oy>=0;--oy){
				if(empty>map[oy][x]){
#ifdef DEBUG
	std::cout <<"PASSy "<<x<<' '<<y<<"<->"<<x<<' '<<oy<<' '<<map[oy][x]<<"\\n";
#endif
					*(iter++) = map[oy][x];
					pushback(graph[map[oy][x]][3],map[y][x]);
#ifdef DEBUG
	std::cout <<"graph "<<*(iter-1) <<' '<<map[y][x]<<"\\n";
#endif
				}
			}
	}
	int bit = 0;
	while(--count>=0){bit<<=1;bit|=1;}
#ifdef DEBUG
	std::cout<<bit<<std::endl;
	for(auto& g:graph[goal])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
	for(auto& g:graph[4])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
	for(auto& g:graph[3])for(auto iter=g;iter!=g+5;++iter){
	std::cout << *iter << ' ';
	}std::cout<<std::endl;
#endif
	int res = 0;
	for(auto& arr : graph[goal]){
		for(auto iter=arr;*iter!=empty;++iter){
			res+=func(*iter,bit&(~(1<<*iter)));
		}
	}
	std::cout << res << std::endl;
	
	}
}
#endif