| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
一个很诧异的问题求解主要是注释那个问题。。。
#include <cstdio>
#include <cstring>
#define maxn 24
using namespace std;
int sx,sy,ex,ey;
bool flag;
int ans,n,m,map[maxn][maxn];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int len)
{
int i,tx,ty;
if (len > 10) return ;
for (i = 0; i < 4; ++i)
{
tx = x + dir[i][0];
ty = y + dir[i][1];
if (tx >= 0 && tx < n && ty >= 0 && ty < m && map[tx][ty] != 1)
{
while ((map[tx][ty] == 0 || map[tx][ty] == 2) && tx >= 0 && tx < n && ty >= 0 && ty < m)
{
tx += dir[i][0];
ty += dir[i][1];
}
if (map[tx][ty] == 3)
{
if (ans > len) ans = len;
flag = true;
return ;
}
else if (map[tx][ty] == 1)
{
map[tx][ty] = 0;
dfs(tx - dir[i][0],ty - dir[i][1],len + 1);
map[tx][ty] = 1;
}
}
}
}
int main()
{
//freopen("d.in","r",stdin);
int i,j;
while (scanf("%d%d",&m,&n))
{
if (!n && !m) break;
memset(map,0,sizeof(map));
for (i = 0; i < n; ++i)
{
for (j = 0; j < m; ++j)
{
scanf("%d",&map[i][j]);
if (map[i][j] == 2)
{
sx = i; sy = j;
}
/*if (map[i][j] == 3)
{
ex = i; ey = j;
}*/ // 这一段在程序中根本没有用到,我带着这段代码提交wa去了这段代码AC求解啊??
}
}
flag = false; ans = 15;
dfs(sx,sy,1);
if (flag) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator