| ||||||||||
| 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 | |||||||||
怎么老是WA啊?#include<iostream>
using namespace std;
int maze[41][41],maze2[41][41],maze3[41][41],H=0,W=0,end[2],ans=0;
bool isOK=false;
struct point
{
int x,y;
}next;
void findLeast(int x,int y,int step)
{
step++;
maze[x][y]=step;
if(x==end[0] && y==end[1])
{
isOK=true;
ans=step;
}
if(x>0 && !isOK && maze[x-1][y]==0)
{
findLeast(x-1,y,step);
maze[x-1][y]=step;
}
if(y>0 && !isOK && maze[x][y-1]==0)
{
findLeast(x,y-1,step);
maze[x][y-1]=step;
}
if(x<H-1 && !isOK && maze[x+1][y]==0)
{
findLeast(x+1,y,step);
maze[x+1][y]=step;
}
if(y<W-1 && !isOK && maze[x][y+1]==0)
{
findLeast(x,y+1,step);
maze[x][y+1]=step;
}
}
bool isDirectOK(int x,int y,int direct,point &n, int m[41][41])
{
if(direct==0 && y>0)
{
if(m[x][y-1]>=0)
{
n.x=x;
n.y=y-1;
return true;
}
}else if(direct==1 && x>0)
{
if(m[x-1][y]>=0)
{
n.x=x-1;
n.y=y;
return true;
}
}else if(direct==2 && y<W-1)
{
if(m[x][y+1]>=0)
{
n.x=x;
n.y=y+1;
return true;
}
}else if(direct==3 && x<H-1)
{
if(m[x+1][y]>=0)
{
n.x=x+1;
n.y=y;
return true;
}
}
return false;
}
void findLeft(int x,int y,int &step,int nowDirect)
{
int leftDirect=(nowDirect+3)%4;
int rightDirect=(nowDirect+5)%4;
int backDirect=(nowDirect+2)%4;
maze2[x][y]=step++;
if(x==end[0] && y==end[1])
{
isOK=true;
}
if(isDirectOK(x,y,leftDirect,next,maze2) && !isOK)
{
findLeft(next.x,next.y,step,leftDirect);
maze2[next.x][next.y]=0;
}
if(nowDirect==0 && !isOK && y>0 && maze2[x][y-1]>=0)
{
findLeft(x,y-1,step,nowDirect);
maze2[x][y-1]=0;
}
if(nowDirect==1 && !isOK && x>0 && maze2[x-1][y]>=0)
{
findLeft(x-1,y,step,nowDirect);
maze2[x-1][y]=0;
}
if(nowDirect==2 && !isOK && y<W-1 && maze2[x][y+1]>=0)
{
findLeft(x,y+1,step,nowDirect);
maze2[x][y+1]=0;
}
if(nowDirect==3 && !isOK && x<H-1 && maze2[x+1][y]>=0)
{
findLeft(x+1,y,step,nowDirect);
maze2[x+1][y]=0;
}
if(isDirectOK(x,y,rightDirect,next,maze2) && !isOK)
{
findLeft(next.x,next.y,step,rightDirect);
maze2[next.x][next.y]=0;
}
if(isDirectOK(x,y,backDirect,next,maze2) && !isOK)
{
findLeft(next.x,next.y,step,backDirect);
maze2[next.x][next.y]=0;
}
}
void findRight(int x,int y,int &step,int nowDirect)
{
int leftDirect=(nowDirect+3)%4;
int rightDirect=(nowDirect+5)%4;
int backDirect=(nowDirect+2)%4;
maze3[x][y]=step++;
if(x==end[0] && y==end[1])
{
isOK=true;
}
if(isDirectOK(x,y,rightDirect,next,maze3) && !isOK)
{
findRight(next.x,next.y,step,rightDirect);
maze3[next.x][next.y]=0;
}
if(nowDirect==0 && !isOK && y>0 && maze3[x][y-1]>=0)
{
findRight(x,y-1,step,nowDirect);
maze3[x][y-1]=0;
}
if(nowDirect==1 && !isOK && x>0 && maze3[x-1][y]>=0)
{
findRight(x-1,y,step,nowDirect);
maze3[x-1][y]=0;
}
if(nowDirect==2 && !isOK && y<W-1 && maze3[x][y+1]>=0)
{
findRight(x,y+1,step,nowDirect);
maze3[x][y+1]=0;
}
if(nowDirect==3 && !isOK && x<H-1 && maze3[x+1][y]>=0)
{
findRight(x+1,y,step,nowDirect);
maze3[x+1][y]=0;
}
if(isDirectOK(x,y,leftDirect,next,maze3) && !isOK)
{
findRight(next.x,next.y,step,leftDirect);
maze3[next.x][next.y]=0;
}
if(isDirectOK(x,y,backDirect,next,maze3) && !isOK)
{
findRight(next.x,next.y,step,backDirect);
maze3[next.x][next.y]=0;
}
}
int findDirect(int x,int y)
{
if(x==0)
{
return 3;
}else if(x==H-1)
{
return 1;
}else if(y==0)
{
return 2;
}else if(y==W-1)
{
return 0;
}
}
int main()
{
int n=0,start[2]={0},firstDirect=0;
char c;
scanf("%d",&n);
for(int x=0;x<n;x++)
{
scanf("%d%d",&W,&H);
memset(maze,0,sizeof(maze));
for(int i=0;i<H;i++)
{
for(int j=0;j<W;)
{
scanf("%c",&c);
if(c=='#')
{
maze[i][j++]=-1;
}else if(c=='.')
{
maze[i][j++]=0;
}else if(c=='S')
{
start[0]=i;
start[1]=j++;
}else if(c=='E')
{
end[0]=i;
end[1]=j++;
}
}
}
memcpy(maze2,maze,sizeof(maze));
memcpy(maze3,maze,sizeof(maze));
firstDirect=findDirect(start[0],start[1]);
isOK=false;
ans=0;
findLeft(start[0],start[1],ans,firstDirect);
printf("%d ",ans);
isOK=false;
ans=0;
findRight(start[0],start[1],ans,firstDirect);
printf("%d ",ans);
ans=0;
isOK=false;
findLeast(start[0],start[1],ans);
printf("%d\n",ans);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator