UVA816Abbott'sRevenge

//UVA816Abbott'sRevenge
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
//#define LOCAL
using namespace std;
struct dot {
	int r, c, face_ID;
	dot(int r = 0, int c = 0, int face_ID = 0) : r(r), c(c), face_ID(face_ID) {}
};
const int MAXR = 10 + 10;
const int MAXC = 10 + 10;
char title[100];
int r0, c0, r1, c1, r2, c2;
char faced0[100];
int got[MAXR][MAXC][10][10];
int d[MAXR][MAXC][10];//记录出发点到该点的距离
dot father[MAXR][MAXC][10];//记录该点上一个点的信息 
dot G0, G1;
const char* faces = "NESW";
const char* go = "LRF";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int tra_fac(char c) {
	return strchr(faces, c) - faces;
}//面对方向的数字转换 
int tra_go(char c) {
	return strchr(go, c) - go;
}//转向操作的数字转换 
bool read() {//读入数据 
	memset(got, 0, sizeof(got));
    memset(d, -1, sizeof(d));
    //memset(father, -1, sizeof(father));
    scanf("%s", title);
    if(!strcmp(title, "END")) return false;
	scanf("%d", &r0); 
	scanf("%d%s%d%d", &c0, faced0, &r2, &c2);
	G0.c = c0; G0.r = r0; G0.face_ID = tra_fac(faced0[0]);
	//printf("G0.r = %d, c = %d\n", G0.r, G0.c);
    G1.c = c0 + dc[G0.face_ID]; G1.r = r0 + dr[G0.face_ID]; G1.face_ID = tra_fac(faced0[0]);
    //printf("G0.face_ID = %d\n", G0.face_ID);
    //printf("G1.r = %d, c = %d\n", G1.r, G1.c);
	int tmp_r, tmp_c;
    char tmp_s[100];
    
	while(scanf("%d", &tmp_r)) {
		if(tmp_r == 0) return true;//输入结束 
		scanf("%d", &tmp_c);
		while(scanf("%s", tmp_s) && tmp_s[0] != '*') {
			for(int i = 1; i < strlen(tmp_s); i++) {
				//printf("tmp_s = %s\n", tmp_s);
				int fa = tra_fac(tmp_s[0]), tmp_got = tra_go(tmp_s[i]);
				//printf("tmp_r = %d, tmp_c = %d, fa = %d, tmp_got = %d\n", tmp_r, tmp_c, fa, tmp_got);
				got[tmp_r][tmp_c][fa][tmp_got] = 1;//printf("***********\n");
				//标记可行路 
			}
		}
	}
	return true;
}
dot walk(dot former, int turn) {//探索下一个结点可能的位置 
	int tmp_faced = former.face_ID;
	if(turn == 0) tmp_faced = (tmp_faced + 3) % 4;//逆时针,防越界  
    if(turn == 1) tmp_faced = (tmp_faced + 1) % 4;//顺时针,防越界
    return dot(former.r + dr[tmp_faced], former.c + dc[tmp_faced], tmp_faced);
}
int Islegal(int r, int c) {
	return r <= 9 && r >= 1 && c <= 9 && c >= 1;
}
void got_ans(dot terminal) {
	vector<dot> ans;
	ans.push_back(terminal);
	dot tmp = terminal;
	while(d[tmp.r][tmp.c][tmp.face_ID] > 0) {
		//printf("(%d, %d) : d = %d\n", tmp.r, tmp.c, d[tmp.r][tmp.c][tmp.face_ID]);
		tmp = father[tmp.r][tmp.c][tmp.face_ID];
		ans.push_back(tmp);
	}
	//ans.push_back(tmp);
	ans.push_back(G0);
	printf("%s\n", title);
	int i = 0;
	for(i = 1; i <= ans.size() ; i++) {
		if(i % 10 == 1) printf(" ");
		printf(" ");
		printf("(%d,%d)", ans[ans.size() - i].r, ans[ans.size() - i].c);
		if(i % 10 == 0) printf("\n");
	}
	if(i % 10 != 1) printf("\n");
}
bool solve() {
	queue<dot> routine;//
	routine.push(G1);//BFS
	d[G1.r][G1.c][G1.face_ID] = 0;
	while(!routine.empty()) {
		dot tmp_routine = routine.front(); routine.pop();
	//	printf("tmp_routine.r = %d, c = %d\n", tmp_routine.r, tmp_routine.c);
		if(tmp_routine.r == r2 && tmp_routine.c == c2) {
			got_ans(tmp_routine); return true;
		}
		for(int i = 0; i < 3; i++) {//探索每个方向 
			dot tmp = walk(tmp_routine, i);
			if(Islegal(tmp.r, tmp.c) && got[tmp_routine.r][tmp_routine.c][tmp_routine.face_ID][i] && d[tmp.r][tmp.c][tmp.face_ID] < 0) {
				//行走未越界,可达,此方向的此点未被访问过
			     d[tmp.r][tmp.c][tmp.face_ID] = d[tmp_routine.r][tmp_routine.c][tmp_routine.face_ID] + 1;
			     father[tmp.r][tmp.c][tmp.face_ID] = tmp_routine;
			     routine.push(tmp);
			}
		}
	}
	printf("%s\n", title);
	printf("  No Solution Possible\n");
}
int main() {
	#ifdef LOCAL
	freopen("UVA816in.txt", "r", stdin);
	freopen("UVA816out.txt", "w", stdout);
	#endif
	while(read()) {
		solve();
	}
}
/*
SAMPLE
3 3 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

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值