Curling 2.0
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12123 | Accepted: 5110 |
#include <iostream>
#include <cstdio>
using namespace std;
int w, h;
int board[20][20];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int ans;
void dfs(int x, int y, int step, int dir)
{
x += dx[dir];
y += dy[dir];
if(0 > x || x >= h || 0 > y || y >= w || step > 10)//wa1:在条件之外,或,被弹出去,
return;
if(board[x][y] == 1)
{
board[x][y] = 0;
for(int i = 0; i < 4; i++)
{
if(board[x - dx[dir] + dx[i]][y - dy[dir] + dy[i]] != 1)//wa2:退回墙 !=1 而不是 ==0
dfs(x - dx[dir], y - dy[dir], step + 1, i);
}
board[x][y] = 1;
}
else if(board[x][y] == 3)
{
if(ans > step)
{
ans = step;
return;
}
}
else
{
dfs(x, y, step, dir);
}
}
int main()
{
while(~scanf("%d%d", &w, &h), w != 0 || h != 0)
{
int start_x, start_y;
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
cin >> board[i][j];
if(board[i][j] == 2)
{
start_x = i;
start_y = j;
}
}
}
ans = 11;
for(int i = 0; i < 4; i++)
{
if(board[start_x + dx[i]][start_y + dy[i]] != 1)//wa3:23的情况过不了
dfs(start_x, start_y, 1, i);
}
if(ans == 11)
ans = -1;
cout << ans << endl;
}
return 0;
}
代码改自http://blog.sina.com.cn/s/blog_6635898a0100htw7.html