816 - Abbott's Revenge

题目用到的思想不难,在紫书上面给出了很好的解析,唯一的细节在path数组存储当前路径到开始顶点的距离,path中的元素全部初始化为-1,同时处理要从题目中给出的初始顶点按照初始方向移动一步后开始。并且将移动之后的那个点的距离初始化为0。如果以题目当中给出的初始顶点以及初始方向开始处理,并且将该相应距离初始化为0,那么后面如果再次以相同的方向到达初始顶点,那么在对距离进行更新的时候就会使得路径中没有初始距离为0的点,从而造成后面利用vector存储相应路径顶点的时候进入死循环,就是在这个点上WA了n次,还好终于找出来了,AC了。。。。。。

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
using namespace std;

struct node{
	int x, y,direction;
	node(int a=0,int b=0,int d=0):x(a),y(b),direction(d){
	}
};

int starx, stary,init_d, endx, endy,initx,inity;
string Dir = "NESW";
string Wl = "FLR";
int dir[4][2] = { {-1,0}, {0,1}, {1,0}, {0,-1} };
int path[10][10][4];
node parent[10][10][4];
int could_turn[10][10][4][3];

int find_dir_D(char aim){
	for (int i = 0; i < 4; i++){
		if (Dir[i] == aim) return i;
	}
}

int find_dir_W(char aim){
	for (int i = 0; i < 3; i++){
		if (Wl[i] == aim) return i;
	}
}

int next_dir(int cur_dir,int turn){
	int aim_dir=cur_dir;
	if (turn == 1) aim_dir = (cur_dir + 3) % 4;
	if (turn == 2) aim_dir = (cur_dir + 1) % 4;
	return aim_dir;
}

bool inside(int x,int y){
	return x > 0 && x < 10 && y > 0 && y < 10;
}

void PrintRes(int x,int y,int d){
	vector<node> res;
	node a(x,y,d);
	res.push_back(a);
	while (true){
		if (path[x][y][d] == 0) break;
		int aa = path[x][y][d];
		res.push_back(parent[x][y][d]);
		int curx, cury, curd;
		curx = parent[x][y][d].x;
		cury = parent[x][y][d].y;
		curd = parent[x][y][d].direction;
		x = curx; y = cury; d = curd;
	}
	int amount = 0;
	node t(starx,stary,init_d);
	res.push_back(t);
	for (int i = res.size() - 1; i >= 0; i--){
		amount++;
		if (amount == 1) cout << "  ";
		amount = amount % 10;
		if (amount&&i!=0){
			cout << "(" << res[i].x << "," << res[i].y << ")" << " ";
		}
		else{
			cout << "(" << res[i].x << "," << res[i].y << ")" << endl;
		}
	}
}

void BFS(){
	node start(initx,inity,init_d);
	path[initx][inity][init_d] = 0;
	node first(starx,stary,init_d);
	parent[initx][inity][init_d] = first;
	queue<node> q;
	q.push(node(initx,inity,init_d));
	while (!q.empty()){
		node temp = q.front();
		if (temp.x == endx&&temp.y == endy){
			PrintRes(temp.x, temp.y, temp.direction);
			return;
		}
		q.pop();
		int curx = temp.x, cury = temp.y, curd = temp.direction;
		for (int i = 0; i < 3; i++){
			int nextd = next_dir(curd, i),nextx = curx + dir[nextd][0], nexty = cury + dir[nextd][1];
			if (could_turn[curx][cury][curd][i]&path[nextx][nexty][nextd]==-1&&inside(nextx,nexty)){	
				path[nextx][nexty][nextd] = path[curx][cury][curd] + 1;
				parent[nextx][nexty][nextd] = temp;
				node temp2(nextx,nexty,nextd);
				q.push(temp2);
			}
		}
	}
	cout << "  No Solution Possible" << endl;
}

int main(){
	char dir1;
	string name;
	while (cin >> name){
		if (name == "END") break;
		    cin >> starx >> stary >> dir1 >> endx >> endy;
			memset(could_turn, 0, sizeof(could_turn));
			memset(path, -1, sizeof(path));
			int x, y;
			while (cin >> x){
				if (x == 0) break;
				cin >> y;
				getchar();
				char a, b;
				while ((a = getchar()) && a != '*'){
					int dir_a = find_dir_D(a);
					while ((b = getchar()) && b != ' '){
						int dir_b = find_dir_W(b);
						could_turn[x][y][dir_a][dir_b] = 1;
					}
				}
			}
			init_d = find_dir_D(dir1);
			initx = starx + dir[init_d][0];
			inity = stary + dir[init_d][1];
			cout << name << endl;
			BFS();
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值