| ||||||||||
| 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 | |||||||||
那位大牛能帮我看一下为什么会wrong啊??????啊啊啊啊啊啊#include <iostream>
#include <queue>
using namespace std;
int f[4][2] =
{
{ 1, 0 },
{ -1, 0 },
{ 0, 1 },
{ 0, -1 } };
int n, m, l;
struct point
{
int res, x[8], y[8];
};
bool isin(point a)
{
if (a.x[0] >= 0 && a.y[0] >= 0 && a.x[0] < n && a.y[0] < m)
{
return true;
}
return false;
}
bool atself(point a,point curr)
{
for(int i = 1;i < l;i++)
{
if(a.x[0]==curr.x[i]&&a.y[0]==curr.y[i])
{
return true;
}
}
return false;
}
bool map[21][21];
bool visit[8][21][21];
bool visited(point temp,point currsnake)
{
point uuu = temp;
for (int j = l - 1; j > 0; j--)
{
uuu.x[j] = currsnake.x[j - 1];
uuu.y[j] = currsnake.y[j - 1];
}
for(int i = 0;i < l;i++)
{
if(!visit[i][uuu.x[i]][uuu.y[i]])
{
return false;
}
}
return true;
}
queue<point> q;
point p, temp,currsnake;
int bfs(point start)
{
memset(visit,0,sizeof(visit));
start.res = 0;
q.push(start);
while (!q.empty())
{
p = q.front();
temp = p;
currsnake = p;
q.pop();
if (p.x[0] == 0 && p.y[0] == 0)
{
return p.res;
}
for (int i = 0; i < 4; i++)
{
temp.x[0] = p.x[0] + f[i][0];
temp.y[0] = p.y[0] + f[i][1];
if (isin(temp) && !map[temp.x[0]][temp.y[0]]
&&!atself(temp,currsnake)&&!visited(temp,currsnake))
{
for (int j = l - 1; j > 0; j--)
{
temp.x[j] = currsnake.x[j - 1];
temp.y[j] = currsnake.y[j - 1];
}
temp.res = p.res + 1;
for(int k = 0;k < l;k++)
{
visit[k][temp.x[k]][temp.y[k]] = 1;
}
// cout<<temp.x[0]+1<<"-----"<<temp.y[0]+1<<"-----"<<temp.res<<endl;
q.push(temp);
}
}
}
return -1;
}
int main()
{
int i;
point start;
int ith = 0;
while (cin >> n >> m >> l && (n && m && l))
{
ith++;
while(!q.empty())
{
q.pop();
}
for(i=0;i<n;i++)
{
for(int j = 0;j < m;j++)
{
map[i][j] = 0;
}
}
int x, y;
for (i = 0; i < l; i++)
{
cin >> x >> y;
x--, y--;
start.x[i] = x;
start.y[i] = y;
}
int stone;
cin >> stone;
for (i = 0; i < stone; i++)
{
cin >> x >> y;
x--, y--;
map[x][y] = 1;
}
cout << "Case " << ith << ": " << bfs(start) << endl;
}
return 0;
}
用visit[8][21][21]记录蛇的状态有问题吗????
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator