Problem:
Problem G:
Walking Ant
Ants are quite diligent. They sometimes build their nests beneath flagstones.
Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest.
The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates (
x
,
y
) at time
t
, she will be on one of the five flagstones with the following coordinates at time
t
+ 1:
(
x
,
y
), (
x
+ 1,
y
), (
x
- 1,
y
), (
x
,
y
+ 1), (
x
,
y
- 1).
The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once.
Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants.
When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home.
If there is a puddle on a flagstone, the ant cannot move there.
Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible.
Input
The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format.
w h
d
11
d
12
d
13
...
d
1
w
d
2
d
22
d
23
...
d
2
w
...
d
h
1
d
h
2
d
h
3
...
d
h
w
The integers
w
and
h
are the numbers of flagstones in the
x
- and
y
-directions, respectively.
w
and
h
are less than or equal to 8. The integer
d
yx
represents the state of the flagstone with coordinates (
x
,
y
) as follows.
0: There is a puddle on the flagstone, and the ant cannot move there.
1, 2: Nothing exists on the flagstone, and the ant can move there. '2' indicates where the ant initially stands.
3: The hole to the nest is on the flagstone.
4: Some food is on the flagstone.
There is one and only one flagstone with a hole. Not more than five flagstones have food on them.
The end of the input is indicated by a line with two zeros.
Integer numbers in an input line are separated by at least one space character.
Output
for each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time.
Sample Input
3 3
2 1 1
1 1 0
1 1 3
8 4
2 1 1 0 1 1 1 0
1 0 4 1 1 0 4 1
1 0 0 0 0 0 0 1
1 1 1 4 1 1 1 3
8 5
1 2 1 1 1 1 1 4
1 0 0 0 1 0 0 1
1 4 1 0 1 
...(truncated)

Submitted solutions:

code_snippet: #include<iostream>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<stack>
#include<functional>
#define INF 2100000000
#define MOD 1000000007
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int w, h;
int mp[10][10];
int hp[10][10];
int t[10][10];
int vis[10][10];
int ans = INF;
int nxt[4][2] = {
	1,0,
	0,-1,
	-1,0,
	0,1
};
void dfs(int x, int y, int hp, int t)
{
	if (hp == 0) return;
	if (mp[x][y] == 3)
	{
		ans = min(ans, t);
		return;
	}
	if (mp[x][y] == 4)
	{
		hp = 6;
		//cout << t << endl;
	}
	for (int i = 0; i < 4; i++)
	{
		int nx = x + nxt[i][0];
		int ny = y + nxt[i][1];
		if (nx > 0 && nx <= h && ny > 0 && ny <= w && mp[nx][ny] != 0 && vis[nx][ny] == 0)
		{
			if (mp[nx][ny] == 4) vis[nx][ny] = 1;;
			dfs(nx, ny, hp - 1, t + 1);
			if (mp[nx][ny] == 4) vis[nx][ny] = 0;
		}
	}
}
int main(void)
{
	while (scanf("%d %d", &w, &h) == 2)
	{
		if (w == 0) break;
		int x = 0, y = 0;
		ans = INF;
		for (int i = 1; i <= h; i++)
		{
			for (int j = 1; j <= w; j++)
			{
				scanf("%d", &mp[i][j]);
				if (mp[i][j] == 2) x = i, y = j;
			}
		}
		dfs(x, y, 6, 0);
		if (ans == INF) printf("-1\n");
		else printf("%d\n", ans);
	}
	return 0;
}
accuracy: 1
time_ms: 6020
memory_kb: 3204
final_rank: 6

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


struct Info{
	Info(){
		eat_after = row = col = hp = time = 0;
	}

	void set(int arg_row,int arg_col,int arg_hp,int arg_time,int arg_eat_after){
		row = arg_row;
		col = arg_col;
		hp = arg_hp;
		time = arg_time;
		eat_after = arg_eat_after;
	}

	int row,col,hp,time,eat_after;
	short check[8][8];
};

struct Data{
	int row,col;
};

int W,H,div_row[4] = {-1,0,0,1},div_col[5] = {0,-1,1,0};
bool isCrearable;

Data data[7];
int data_index,distMap[8][8],map[8][8];
int start_row,start_col,goal_row,goal_col;


bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void dist5(int row,int col){
	int next_row,next_col;
	for(int i = 0; i < 4; i++){
		next_row = row+div_row[i],next_col = col+div_col[i];
		if(rangeCheck(next_row,next_col) == true && map[next_row][next_col] != 0 && distMap[next_row][next_col] > distMap[row][col]+1 && distMap[row][col] <= 4){
			distMap[next_row][next_col] = min(distMap[next_row][next_col],distMap[row][col]+1);
			if(map[next_row][next_col] == 4){
				distMap[next_row][next_col] = 0;
				dist5(next_row,next_col);
			}else if(next_row == start_row && next_col == start_col){
				isCrearable = true;
				return;
			}else{
				dist5(next_row,next_col);
			}
		}
	}
}

void func(){

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++){
			scanf("%d",&map[i][k]);
			if(map[i][k] == 2){
				start_row = i;
				start_col = k;
				map[i][k] = 1;
			}else if(map[i][k] == 3){
				goal_row = i;
				goal_col = k;
			}
		}
	}

	isCrearable = false;
	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++)distMap[i][k] = BIG_NUM;
	}

	distMap[goal_row][goal_col] = 0;
	data_index = 0;

	dist5(goal_row,goal_col);

	if(isCrearable == false){
		printf("-1\n");
		return;
	}

	int ans = -1,limit = 36;
	queue<Info> Q;
	Info current;

	Info first;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++)first.check[i][k] = 0;
	}

	first.check[start_row][start_col] = 1;
	first.set(start_row,start_col,6,0,3);

	Q.push(first);

	while(!Q.empty()){
		current = Q.front();
		Q.pop();

		if(current.hp == 0)continue;

		if(map[current.row][current.col] == 3){
			ans = current.time;
			break;
		}else if(map[current.row][current.col] == 4){
			current.hp = 6;
			current.eat_after = 0;
		}

		if(current.time + abs(current.row-goal_row)+abs(current.col-goal_col) > limit)continue;

		for(int i = 0; i < 4; i++){
			if(rangeCheck(current.row+div_row[i],current.col+div_col[i]) == true && map[current.row+div_row[i]][current.col+div_col[i]] != 0 &&
					((current.check[current.row+div_row[i]][current.col+div_col[i]] == 0) || (current.check[current.row+div_row[i]][current.col+div_col[i]] == 1 &&	current.eat_after <= 1))){
				Info new_info;
				for(int a = 0; a < H; a++){
					for(int b = 0; b < W; b++){
						new_info.check[a][b] = current.check[a][b];
					}
				}
				new_info.check[current.row+div_row[i]][current.col+div_col[i]]++;
				new_info.set(current.row+div_row[i],current.col+div_col[i],current.hp-1,current.time+1,current.eat_after+1);
				Q.push(new_info);
			}
		}
	}
	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 24036
final_rank: 3

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int W,H,div_row[4] = {-1,0,0,1},div_col[5] = {0,-1,1,0};

struct Info{
	Info(){
		eat_after = row = col = hp = time = 0;
	}

	void set(int arg_row,int arg_col,int arg_hp,int arg_time,int arg_eat_after){
		row = arg_row;
		col = arg_col;
		hp = arg_hp;
		time = arg_time;
		eat_after = arg_eat_after;
	}

	int row,col,hp,time,eat_after;
	short check[8][8];
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	int map[H][W];
	int start_row,start_col,goal_row,goal_col;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++){
			scanf("%d",&map[i][k]);
			if(map[i][k] == 2){
				start_row = i;
				start_col = k;
				map[i][k] = 1;
			}else if(map[i][k] == 3){
				goal_row = i;
				goal_col = k;
			}
		}
	}

	int ans = -1,limit = 2*H*W;
	queue<Info> Q;
	Info current;

	Info first;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++)first.check[i][k] = 0;
	}

	first.check[start_row][start_col] = 1;
	first.set(start_row,start_col,6,0,3);

	Q.push(first);

	int debug = 0;
	while(!Q.empty()){
		current = Q.front();
		Q.pop();

		debug++;
		if(debug > 240000){ //I'm very sorry,I'm tired. I will submit an appropriate code someday.
			break;
		}

		if(current.hp == 0)continue;

		if(map[current.row][current.col] == 3){
			ans = current.time;
			break;
		}else if(map[current.row][current.col] == 4){
			current.hp = 6;
			current.eat_after = 0;
		}

		if(current.time + abs(current.row-goal_row)+abs(current.col-goal_col) > limit)continue;

		for(int i = 0; i < 4; i++){
			if(rangeCheck(current.row+div_row[i],current.col+div_col[i]) == true && map[current.row+div_row[i]][current.col+div_col[i]] != 0 &&
					((current.check[current.row+div_row[i]][current.col+div_col[i]] == 0) || (current.check[current.row+div_row[i]][current.col+div_col[i]] == 1 &&	current.eat_after <= 1))){
				Info new_info;
				for(int a = 0; a < H; a++){
					for(int b = 0; b < W; b++){
						new_info.check[a][b] = current.check[a][b];
					}
				}
				new_info.check[current.row+div_row[i]][current.col+div_col[i]]++;
				new_info.set(current.row+div_row[i],current.col+div_col[i],current.hp-1,current.time+1,current.eat_after+1);
				Q.push(new_info);
			}
		}
	}
	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 56652
final_rank: 4

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int W,H,div_row[4] = {-1,0,0,1},div_col[5] = {0,-1,1,0};

struct Info{
	Info(){
		eat_after = row = col = hp = time = 0;
	}

	void set(int arg_row,int arg_col,int arg_hp,int arg_time,int arg_eat_after){
		row = arg_row;
		col = arg_col;
		hp = arg_hp;
		time = arg_time;
		eat_after = arg_eat_after;
	}

	int row,col,hp,time,eat_after;
	short check[8][8];
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= H-1 && col >= 0 && col <= W-1)return true;
	else{
		return false;
	}
}

void func(){

	int map[H][W];
	int start_row,start_col,goal_row,goal_col;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++){
			scanf("%d",&map[i][k]);
			if(map[i][k] == 2){
				start_row = i;
				start_col = k;
				map[i][k] = 1;
			}else if(map[i][k] == 3){
				goal_row = i;
				goal_col = k;
			}
		}
	}

	int ans = -1,limit = 2*H*W;
	queue<Info> Q;
	Info current;

	Info first;

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++)first.check[i][k] = 0;
	}

	first.check[start_row][start_col] = 1;
	first.set(start_row,start_col,6,0,3);

	Q.push(first);

	int debug = 0;
	while(!Q.empty()){
		current = Q.front();
		Q.pop();

		debug++;
		if(debug > 250000){
			break;
		}

		if(current.hp == 0)continue;

		if(map[current.row][current.col] == 3){
			ans = current.time;
			break;
		}else if(map[current.row][current.col] == 4){
			current.hp = 6;
			current.eat_after = 0;
		}

		if(current.time + abs(current.row-goal_row)+abs(current.col-goal_col) > limit)continue;

		for(int i = 0; i < 4; i++){
			if(rangeCheck(current.row+div_row[i],current.col+div_col[i]) == true && map[current.row+div_row[i]][current.col+div_col[i]] != 0 &&
					((current.check[current.row+div_row[i]][current.col+div_col[i]] == 0) || (current.check[current.row+div_row[i]][current.col+div_col[i]] == 1 &&	current.eat_after <= 1))){
				Info new_info;
				for(int a = 0; a < H; a++){
					for(int b = 0; b < W; b++){
						new_info.check[a][b] = current.check[a][b];
					}
				}
				new_info.check[current.row+div_row[i]][current.col+div_col[i]]++;
				new_info.set(current.row+div_row[i],current.col+div_col[i],current.hp-1,current.time+1,current.eat_after+1);
				Q.push(new_info);
			}
		}
	}
	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d %d",&W,&H);
		if(W == 0 && H == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 58864
final_rank: 5

code_snippet: #include<iostream>
#include<queue>
#include<map>
#include<set>

using namespace std;

const int H = 16;
const int W = 16;

int M[H][W];

struct state{
  int i,j;
  int food;
  int hp;
  int cost;
  state(int i, int j, int food, int hp, int cost):i(i),j(j),food(food),hp(hp),cost(cost){}
  bool operator<(const state &t)const{
    if(i!=t.i)return i<t.i;
    if(j!=t.j)return j<t.j;
    if(food!=t.food)return food<t.food;
    if(hp!=t.hp)return hp<t.hp;
    if(cost!=t.cost)return cost<t.cost;
    return false;
  }
};


const int di[] = {0,-1,0,1,0};
const int dj[] = {0,0,1,0,-1};
const int inf = 1<<22;

bool inside(int i, int j, int h, int w){
  return 0<=i&&i<h&&0<=j&&j<w;
}

int solve(int h, int w){
  int si, sj, gi, gj;
  int fid = 0;
  int ret = inf;
  set<state> vis;
  map<pair<int,int>,int> food_conv;
  map<int,pair<int,int> > food_rev;
  
  for(int i = 0; i < h; ++i){
    for(int j = 0; j < w; ++j){
      if( M[i][j] == 2 ){
        si = i;
        sj = j;
      }else if( M[i][j] == 3 ){
        gi = i;
        gj = j;
      }else if( M[i][j] == 4 ){
        food_conv[make_pair(i,j)] = fid;
        food_rev[fid] = make_pair(i,j);
        ++fid;
      }
    }
  }
  state init(si,sj,0,6,0);

  queue<state> q;
  for(q.push(init); !q.empty(); q.pop()){
    state cur = q.front();

    // if( cur.hp == 0 ) continue;

    if( cur.i == gi && cur.j == gj ){
      ret = min(ret, cur.cost );
    }

    if( vis.find(cur) == vis.end() ) vis.insert(cur);
    else continue;

    for(int k = 0; k < 5; ++k){
      int ni = cur.i + di[k];
      int nj = cur.j + dj[k];
      
      if( inside(ni,nj,h,w) && M[ni][nj] != 0 ){
        int nhp = cur.hp - 1;
        int nfood = cur.food;

        if( nhp > 0 ){

          if( M[ni][nj] == 4 ){
            int fid = food_conv[make_pair(ni,nj)];
            if( !(cur.food & (1<<fid)) ){
              nhp = 6;
              nfood |= 1<<fid;
            }
          }
          int ncost = cur.cost + 1;
          q.push(state(ni,nj,nfood,nhp,ncost));
        }
      }
    }
  }

  
  return ret==inf?-1:ret;
}

int main()
{
  int w,h;
  while( cin >> w >> h ){
    if( w == 0 && h == 0 ) break;
    
    for(int i = 0; i < h; ++i){
      for(int j = 0; j < w; ++j){
        cin >> M[i][j];
      }
    }
    
    cout << solve(h,w) << endl;
  }  
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3628
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int d[8][8][8][8];
int sx,sy;
int gx,gy;
int dist[10][10];
int n;

#define MAX 10000

int solve(int now, int flag){
  if(now == n-1) return 0;
  int ret = MAX;
  REP(i,n) if((flag & 1<<i) == 0){
    if(dist[now][i] < 6){
      ret = min(ret, dist[now][i] + solve(i, flag | (1<<i)));
    }
  }
  return ret;
}

int main(){
  int w,h;
  while((w = getInt()) + (h = getInt())){
    int b[h][w];
    deque<pair<int,int> > v;

    REP(i,h) REP(j,w){
      int t = getInt();
      if(t == 2){
	t = 1;
	sx = j; sy = i;
      }else if(t == 3){
	t = 1;
	gx = j; gy = i;
      }else if(t == 4){
	t = 1;
	v.push_back(mp(j,i));
      }
      b[i][j] = t;
    }

    v.push_back(mp(gx,gy));
    v.push_front(mp(sx,sy));

    REP(i,h) REP(j,w) REP(k,h) REP(l,w)
      d[i][j][k][l] = MAX;

    REP(i,h) REP(j,w) d[i][j][i][j] = 0;

    REP(i,h) REP(j,w){
      if(b[i][j] == 0) continue;
      if(i < h-1) if(b[i+1][j] == 1)
	d[i][j][i+1][j] = d[i+1][j][i][j] = 1;
      if(j < w-1) if(b[i][j+1] == 1)
	d[i][j][i][j+1] = d[i][j+1][i][j] = 1;
    }

    REP(k,w*h) REP(i,w*h) REP(j,w*h)
      d[i/w][i%w][j/w][j%w]
      = min(d[i/w][i%w][j/w][j%w],
	    d[i/w][i%w][k/w][k%w] + d[k/w][k%w][j/w][j%w]);

    //REP(i,h) { REP(j,w) printf("%d ",d[sy][sx][i][j]==10000?-1:d[sy][sx][i][j]); puts(""); }

    n = v.size();

    if(n == 0){
      if(d[sy][sx][gy][gx] == MAX)
	print(-1);
      else
	print(d[sy][sx][gy][gx]);
      continue;
    }

    REP(i,n) REP(j,n)
      dist[i][j] = d[v[i].s][v[i].f][v[j].s][v[j].f];
    
    int ans = solve(0,0);

    if(ans >= MAX){
      print(-1);
      continue;
    }

    print(ans);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1