题面
几组 data
4 5
.....
.X...
...*X
X.X..
3
NORTH
WEST
SOUTH
1.out
.....
*X*..
*.*.X
X.X..
5 5
.....
.....
.....
.....
....*
3
2.out
5 5
****.
****.
****.
.....
.....
3
5 4
.*X.
....
....
..X.
....
2
SOUTH
WEST
3.out
..X.
*...
*...
*.X.
*...
5 20
.............X.XXXXX
....XX..XXXXX...*..X
....XXXX.XXX.X......
....................
...................X
7
SOUTH
WEST
EAST
WEST
NORTH
EAST
SOUTH
4.out
.............X.XXXXX
.***XX**XXXXX......X
.***XXXX.XXX.X.****.
.***...........*****
.******************X
分析
这题输入有毒!!!!!!
这题输入有毒!!!!!!
这题输入有毒!!!!!!
重要的话说三遍!!
本人用dfs爆搜,思路很简单,但是就是调不出来,其原因就在于,这道题的输入中两行间有两个回车字符,而样例里面只有一个回车字符!!!!
于是我一开始的按行输入就直接错掉
loop(i,1,r)
{
loop(j,1,c)
{
scanf("%c",&g[i][j]);
if(g[i][j]=='*'){m_x=i;m_y=j;g[i][j]='.';}
}
scanf("\n");
}
后面我加强了输入模块的鲁棒性,用一个while循环过滤掉所有奇奇怪怪的字符(哈哈哈,来一万个换行也不怕)
loop(i,1,r)
{
loop(j,1,c)
{
char safety;
safety=getchar();
while(true)
{
if(safety=='.'||safety=='X'||safety=='*'){g[i][j]=safety;break;}
else safety=getchar();
}
if(g[i][j]=='*'){m_x=i;m_y=j;g[i][j]='.';}
}
}
于是
code(dfs)
#include<bits/stdc++.h>
using namespace std;
#define loop(i,start,end) for(int i=start;i<=end;i++)
#define clean(arry,num); memset(arry,num,sizeof(arry));
#define printG(); loop(i,0,25){loop(j,0,25)printf("%c ",g[i][j]);printf("\n");}printf("\n");
#define printG2(); loop(i,0,25){loop(j,0,25)printf(" %d ",(a[i][j]==0)?8:a[i][j]);printf("\n");}printf("\n");
#define abs(a) ((a>0)?a:-a)
int r,c,n;
const int maxr=100;//hang
const int maxc=100;//lie
const int maxs=1010;
char g[maxr][maxc];
int a[maxr][maxc];
bool vis[maxr][maxc][maxs];
char str[maxs][10];
int m_x,m_y;
inline int read()
{
int ans=0;char r=getchar();int neg=1;
while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
while(r>='0'&&r<='9'){ans=ans*10+r-'0';r=getchar();}
return ans*neg;
}
void datasetting()
{
r=read();c=read();
loop(i,1,r)
{
loop(j,1,c)
{
char safety;
safety=getchar();
while(true)
{
if(safety=='.'||safety=='X'||safety=='*'){g[i][j]=safety;break;}
else safety=getchar();
}
if(g[i][j]=='*'){m_x=i;m_y=j;g[i][j]='.';}
}
}
clean(g[0],'X');clean(g[r+1],'X');loop(i,0,r+1)g[i][0]=g[i][c+1]='X';
n=read();
loop(i,1,n)scanf("%s",str[i]);
clean(str[0],0);
}
void dfs(int x,int y,int nowstr)
{
if(nowstr==n)
{
a[x][y]=1;
return;
}
int ord=str[nowstr+1][0];
if(ord=='E'||ord=='W')//change y
{
int pos_y=y;
pos_y+=(ord=='E')?1:-1;
while(g[x][pos_y]!='X')
{
if(vis[x][pos_y][nowstr])goto jump0;
vis[x][pos_y][nowstr]=true;
dfs(x,pos_y,nowstr+1);
jump0:pos_y+=(ord=='E')?1:-1;
}
}
else if(ord=='N'||ord=='S')//change x
{
int pos_x=x;
pos_x+=(ord=='N')?-1:1;
while(g[pos_x][y]!='X')
{
if(vis[pos_x][y][nowstr])goto jump1;
vis[pos_x][y][nowstr]=true;
dfs(pos_x,y,nowstr+1);
jump1:pos_x+=(ord=='S')?1:-1;
}
}
}
int main()
{
//freopen("datain.txt","r",stdin);
datasetting();
clean(a,0);clean(vis,false);
dfs(m_x,m_y,0);
loop(i,1,r)
{
loop(j,1,c)
{
if(a[i][j]==1)g[i][j]='*';
printf("%c",g[i][j]);
}
printf("\n");
}
return 0;
}
学到的东西
1.输入的鲁棒性!!!
2.染色标记不要标记错了!!!
要用
bool vis[x][y][i]
来标记,否则答案会不全