错误案例1
#include<bits/stdc++.h>
using namespace std;
int Vis[205][205];
char MAP[205][205];
const int inf=0x3f3f3f3f;
int minhh(int x,int y)
{
if(x<=y)return x;
else return y;
}
struct glass
{
int x,y,steps;
}gg;
int main()
{
int N,M,i,j;
queue<struct glass>q;
queue<struct glass>p;
while(scanf("%d %d",&N,&M)!=EOF){
getchar();//清空输入流,不然会输出错误
for(j=1;j<=N;j++)
for(i=1;i<=M+1;i++)//连续录入字符要注意换行符问题,也占据一个变量
{
scanf("%c",&MAP[j][i]);
if(MAP[i][j]=='a'){gg.x=i;gg.y=j;gg.steps=0;}//录入天使起始位置
}
memset(Vis,0,sizeof(Vis));
q.push(gg);
while(!q.empty())
{
gg=q.front();
q.pop();
if(MAP[gg.x][gg.y]=='r'){i=-1;
break;}
if(Vis[gg.x][gg.y]==0)
{
Vis[gg.x][gg.y]=1;
gg.x--;gg.steps++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.x++;
gg.x++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.x--;
gg.y++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.y--;
gg.y--;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.y++;gg.steps--;
while(!p.empty())
{
gg=p.front();
q.push(gg);
p.pop();
}//把遇到守卫的情况往下压一层
}
}
if(i==-1)printf("%d\n",gg.steps);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
while(!q.empty())//清空队列
{
q.pop();
}
}
return 0;
}
总结
1.这里的本意是利用增加一个p队列存储遇到X时,层数对应不上的情况,但实际上这样写不够全面,比如当你对一个steps为12的叶子判断时,他会产生几个steps为13的叶子,而你把因为遇到X导致的steps为14的叶子放在steps为13的叶子后面,本来没问题,但是判断下一个steps为12的叶子时,就会产生新的steps为13的叶子,会跟在steps为14的叶子后面,导致答案出错
错误案例二(还是不够全面)
#include<bits/stdc++.h>
using namespace std;
int Vis[205][205];
char MAP[205][205];
const int inf=0x3f3f3f3f;
int minhh(int x,int y)
{
if(x<=y)return x;
else return y;
}
struct glass
{
int x,y,steps;
}gg,kk;
int main()
{
int N,M,i,j;
queue<struct glass>q;
queue<struct glass>p;
while(scanf("%d %d",&N,&M)!=EOF){
getchar();//清空输入流,不然会输出错误
for(j=1;j<=N;j++)
for(i=1;i<=M+1;i++)//连续录入字符要注意换行符问题,也占据一个变量
{
scanf("%c",&MAP[j][i]);
if(MAP[i][j]=='a'){gg.x=i;gg.y=j;gg.steps=0;}//录入天使起始位置
}
memset(Vis,0,sizeof(Vis));
q.push(gg);
while(!q.empty())
{
gg=q.front();
q.pop();
if(MAP[gg.x][gg.y]=='r'){i=-1;
break;}
if(Vis[gg.x][gg.y]==0)
{
Vis[gg.x][gg.y]=1;
gg.x--;gg.steps++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.x++;
gg.x++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.x--;
gg.y++;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.y--;
gg.y--;
if(gg.x>=1&&gg.x<=M&&gg.y>=1&&gg.y<=N)
if(MAP[gg.x][gg.y]=='.'||MAP[gg.x][gg.y]=='r')q.push(gg);
else if(MAP[gg.x][gg.y]=='x'){gg.steps++;p.push(gg);gg.steps--;}
gg.y++;gg.steps--;
if(!q.empty())
{
kk=q.front();gg=p.front();
if(kk.steps==gg.steps-1)
while(!p.empty())
{
gg=p.front();
q.push(gg);
p.pop();
}
}
if(q.empty())while(!p.empty())
{
gg=p.front();
q.push(gg);
p.pop();
}//把遇到守卫的情况往下压一层
}
}
if(i==-1)printf("%d\n",gg.steps);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
while(!q.empty())//清空队列
{
q.pop();
}
}
return 0;
}