紫书说必须掌握的题 救命!!
查看原帖
紫书说必须掌握的题 救命!!
106619
yagyagyag楼主2020/7/9 21:03
#include<bits/stdc++.h>
using namespace std;
int sx,sy,ex,ey;
char Direct;
bool D[15][15][5][5];
int pre[1000005];
int turn_id(char c)
{
	if (c=='F') return 0;
	if (c=='L') return 1;
	if (c=='R') return 2;
}
int dir_id(char c)
{
	if (c=='N') return 0;
	if (c=='E') return 1;
	if (c=='S') return 2;
	if (c=='W') return 3;
}
bool flag=0;
int ans[10005][2],tot=0;
struct node{
	int x,y,step,dir,index;
	node(int X=0,int Y=0,int STEP=0,int DIR=0,int INDEX=0):x(X),y(Y),step(STEP),dir(DIR),index(INDEX) {}
};
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
node Turn_Dir(node u,int turn)
{
	node t;t.dir=u.dir;
	pre[tot]=u.index;
	if (turn==1) t.dir=(u.dir+3)%4;
	if (turn==2) t.dir=(u.dir+1)%4;
	t.step=u.step+1;
	t.index=tot;
	t.x=u.x+dx[t.dir];t.y=u.y+dy[t.dir];
	ans[tot][0]=t.x;ans[tot][1]=t.y;tot++;
	return t;
}
bool visit[15][15][15];
void print(int now)
{
	vector<int> A;
	for (;;){
		if (now==-1) break;
		A.push_back(now);
		now=pre[now];
	}
	int cnt=0;
	for (int i=A.size()-1;i>=0;i--){
		printf("(%d,%d) ",ans[A[i]][0],ans[A[i]][1]);
		cnt++;
		if (cnt%10==0) cout<<endl; 
	}
	if (cnt%10) cout<<endl;
}
void bfs()
{
	queue<node> q;
	q.push(node(sx,sy,0,dir_id(Direct),0));
	pre[0]=-1;ans[0][0]=sx;ans[0][1]=sy; 
	while (!q.empty())
	{
		node u=q.front(); q.pop();
//		cout<<u.x<<" "<<u.y<<" "<<u.dir<<" ";
		if (u.x==ex && u.y==ey && u.step!=0 && u.step!=1){
			print(u.index);
			flag=1;return;
		}
		for (int i=0;i<=2;i++){
			if (D[u.x][u.y][u.dir][i]){
				node v=Turn_Dir(u,i);
//				cout<<i<<" ";
//				cout<<v.x<<" "<<v.y<<" "<<v.dir<<"\n";
				if (!visit[v.x][v.y][v.dir]){
					visit[v.x][v.y][v.dir]=1;
					q.push(v);
				}
			}
		}		
//		cout<<endl;
	}
}
int main()
{
//	freopen("1.txt","w",stdout);
	string s;
	while (cin>>s){
		if (s=="END") break;
		memset(D,0,sizeof D);
		memset(pre,0,sizeof pre);
		memset(visit,0,sizeof visit);
		tot=1;
		cin>>sx>>sy>>Direct>>ex>>ey;
		D[sx][sy][dir_id(Direct)][0]=1;
		int x,y,len;string t;
		while (cin>>x){
			if (x==0) break;
			cin>>y;
			while (cin>>t){
				if (t=="*") break;
				len=t.size();
				for (int i=1;i<len;i++)
					D[x][y][dir_id(t[0])][turn_id(t[i])]=1;
			}
		}
//		cout<<"  "<<D[1][2][3][0]<<" "<<D[1][2][3][1]<<" "<<D[1][2][3][2]<<endl;
		cout<<s<<endl;flag=0;
		bfs();
		if (!flag) cout<<"No Solution Possible\n";
	}
	return 0;
}

uDebug网站上测了一下,就test5错了,请大佬们指点

测试数据

SAMPLE
3 1 N 3 3
1 1 WL NR *
1 2 WLF NR ER *
1 3 NL ER *
2 1 SL WR NF *
2 2 SL WF ELF *
2 3 SFR EL *
0
NOSOLUTION
3 1 N 3 2
1 1 WL NR *
1 2 NL ER *
2 1 SL WR NFR *
2 2 SR EL *
0
MyMaze1
3 1 N 1 1
0
MyMaze2
3 1 N 3 1
0
MyMaze3
3 1 N 2 1
0
MyMaze4
2 2 W 3 2
1 1 NR *
1 2 ER *
2 1 WR *
2 2 SF *
0
MyMaze5
2 2 N 2 3
1 1 WL *
1 2 NL *
2 1 SF *
2 2 NR *
3 1 SL *
3 2 EL *
0
Circle
2 1 N 2 1
1 1 NR *
1 2 ER *
2 2 SF *
3 1 WR *
3 2 SR *
0
RobertAbbottAtlanta
4 2 N 4 3
1 1 NR WL *
1 2 NLR WF EFR *
1 3 EFR WFR NL *
1 4 ER NL *
2 1 SFL WL NFR *
2 2 EL SFLR WFRL NFL *
2 3 EFLR SF NF WFRL *
2 4 SR ELR NF *
3 1 WR SL *
3 2 EFL SLR WR NF *
3 3 EFL SLR WL *
3 4 EL SR *
0
END

正确输出

SAMPLE
  (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1)
  (2,2) (1,2) (1,3) (2,3) (3,3)
NOSOLUTION
  No Solution Possible
MyMaze1
  No Solution Possible
MyMaze2
  No Solution Possible
MyMaze3
  (3,1) (2,1)
MyMaze4
  (2,2) (2,1) (1,1) (1,2) (2,2) (3,2)
MyMaze5
  (2,2) (1,2) (1,1) (2,1) (3,1) (3,2) (2,2) (2,3)
Circle
  (2,1) (1,1) (1,2) (2,2) (3,2) (3,1) (2,1)
RobertAbbottAtlanta
  (4,2) (3,2) (2,2) (1,2) (1,3) (1,4) (2,4) (2,3) (2,2) (3,2)
  (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (2,4) (3,4) (3,3) (4,3)

我的输出

SAMPLE
(3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1) 
(2,2) (1,2) (1,3) (2,3) (3,3) 
NOSOLUTION
No Solution Possible
MyMaze1
No Solution Possible
MyMaze2
No Solution Possible
MyMaze3
(3,1) (2,1) 
MyMaze4
(2,2) (2,1) (1,1) (1,2) (2,2) (3,2) 
MyMaze5
(2,2) (2,3) 
Circle
(2,1) (1,1) (1,2) (2,2) (3,2) (3,1) (2,1) 
RobertAbbottAtlanta
(4,2) (3,2) (2,2) (1,2) (1,3) (1,4) (2,4) (2,3) (2,2) (3,2) 
(3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (2,4) (3,4) (3,3) (4,3) 

2020/7/9 21:03
加载中...