poj 2935 Basic Wall Maze bfs

http://poj.org/problem?id=2935

像這樣的題還是要多練啊,不多說。還有許多需要改進的地方

#include <stdio.h>
#include <queue>
#include <string>
#include <iostream>
#include <string.h>

using namespace std;

struct node
{
	int tlr, tlc;
	int trr, trc;

	int blr, blc;
	int brr, brc;
	string str;//方向
	int step;//步數
};

bool map[7][7][7][7];//標記哪些位置不可以走
bool used[7][7][7][7][7][7][7][7];//標記哪些位置的某一方向上是否走過

node start, end;
//判出
bool isout(node t)
{
	if(t.blc == end.blc && t.blr == end.blr && t.brc == end.brc && t.brr == end.brr &&
		t.tlc == end.tlc && t.tlr == end.tlr && t.trc == end.trc && t.trr == end.trr)return true;
	return false;
}

bool isable(node a)
{
	if(a.tlc >= 0 && a.brc <= 6 && a.tlr >= 0 && a.brr <= 6)return true;
	return false;
}

void bfs()
{
	queue<node >q;
	node now;
	q.push(start);
	while(!q.empty()){
		now = q.front();
		q.pop();
		if(isout(now)){
			cout<<now.str<<endl;
			return ;
		}
		//up
		if(map[now.tlr][now.tlc][now.trr][now.trc]){
			node tm;
			tm = now;
			tm.blr -= 1, tm.brr -= 1;
			tm.tlr -= 1, tm.trr -= 1;
			tm.str += 'N';
			tm.step += 1;
			if(isable(tm) && !used[tm.blr][tm.blc][tm.brr][tm.brc][tm.tlr][tm.tlc][tm.trr][tm.trc]){
				used[tm.blr][tm.blc][tm.brr][tm.brc][tm.tlr][tm.tlc][tm.trr][tm.trc] = true;
				q.push(tm);
			}
		}
		//down
		if(map[now.blr][now.blc][now.brr][now.brc]){
			node tm;
			tm = now;
			tm.blr += 1, tm.brr += 1;
			tm.tlr += 1, tm.trr += 1;
			tm.str += 'S';
			tm.step += 1;
			if(isable(tm) && !used[tm.tlr][tm.tlc][tm.trr][tm.trc][tm.blr][tm.blc][tm.brr][tm.brc]){
				used[tm.tlr][tm.tlc][tm.trr][tm.trc][tm.blr][tm.blc][tm.brr][tm.brc] = true;
				q.push(tm);
			}
		}
		//left
		if(map[now.tlr][now.tlc][now.blr][now.blc]){
			node tm;
			tm = now;
			tm.tlc -= 1, tm.trc -= 1;
			tm.blc -= 1, tm.brc -= 1;
			tm.str += 'W';
			tm.step += 1;
			if(isable(tm) && !used[tm.trr][tm.trc][tm.brr][tm.brc][tm.tlr][tm.tlc][tm.blr][tm.blc]){
				used[tm.trr][tm.trc][tm.brr][tm.brc][tm.tlr][tm.tlc][tm.blr][tm.blc] = true;
				q.push(tm);
			}
		}
		//right
		if(map[now.trr][now.trc][now.brr][now.brc]){
			node tm;
			tm = now;
			tm.tlc += 1, tm.trc += 1;
			tm.blc += 1, tm.brc += 1;
			tm.str += 'E';
			tm.step += 1;
			if(isable(tm) && !used[tm.tlr][tm.tlc][tm.blr][tm.blc][tm.trr][tm.trc][tm.brr][tm.brc]){
				used[tm.tlr][tm.tlc][tm.blr][tm.blc][tm.trr][tm.trc][tm.brr][tm.brc] = true;
				q.push(tm);
			}
		}
		
	}
}

int main()
{
	int i, j;
	int lc, lr, rc, rr;
	while(scanf("%d%d", &start.brc, &start.brr)!=EOF){
		if(start.brc == 0 && start.brr == 0)break;
		start.blc = start.brc - 1, start.blr = start.brr;
		start.trc = start.brc, start.trr = start.brr - 1;
		start.tlc = start.blc, start.tlr = start.blr - 1;
		scanf("%d%d", &end.brc, &end.brr);
		end.blc = end.brc - 1, end.blr = end.brr;
		end.trc = end.brc, end.trr = end.brr - 1;
		end.tlc = end.blc, end.tlr = end.blr - 1;

		memset(map, true, sizeof(map));
		for(i = 0; i < 3; i ++){
			scanf("%d%d%d%d", &lc, &lr, &rc, &rr);
			if(lc == rc){
				if(lr > rr){
					for(j = rr; j < lr; j ++){
						map[j][lc][j + 1][lc] = false;
					}
				}
				else if(rr > lr){
					for(j = lr; j < rr; j ++){
						map[j][lc][j + 1][lc] = false;
					}
				}
			}
			else if(lr == rr){
				if(lc > rc){
					for(j = rc; j < lc; j ++){
						map[lr][j][lr][j + 1] = false;
					}
				}
				else if(rc > lc){
					for(j = lc; j < rc; j ++){
						map[lr][j][lr][j + 1] = false;
					}
				}
			}
		}
		start.str = "";
		start.step = 0;
		memset(used, false, sizeof(used));
		bfs();
	}
	return 0;
}


發佈了79 篇原創文章 · 獲贊 20 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章