UVA - 10384The Wall Pushers推门游戏(迭代加深)

题意:从s处出发,每次可以往东南西北4个方向之一前进。如果前方有墙壁,可以把墙壁往前推一格。如果有两堵墙或者多堵墙,则不能移动,另外也不能推边上的墙。找出最短路径。

分析:刚拿到题目肯定想到是bfs,但是由于地图较大,bfs超时

#include<iostream>
#include<time.h>
#include<queue>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node {
	int r, c;
	int ans=0;
	int map[10][10];
	char route[100];
};
int direction[] = { 1,2,4,8 };
int dr[] = { 0,-1,0,1 };
int dc[] = { -1,0,1,0 };
char rr[] = { 'W','N','E','S' };
int row, col;
int mp[10][10];
void bfs() {
	queue<node> q;
	node t;
	t.r = row; t.c = col;
	t.ans = 0;
	memcpy(t.map, mp, sizeof(mp));
	q.push(t);
	while (!q.empty()) {
		t = q.front();
		q.pop();
		if (t.r < 1 || t.r>4 || t.c < 1 || t.c>6) {
			for (int j = 0; j < t.ans; j++) {
				cout << t.route[j];
			}
			cout << endl;
			return;
		}
		row = t.r; col = t.c;
		for (int i = 0; i < 4; i++) {
			node temp;
			temp.ans = t.ans;
			memcpy(temp.map, t.map, sizeof(t.map));
			memcpy(temp.route, t.route, sizeof(t.route));
			int ddr = row, ddc = col;
			ddr += dr[i];
			ddc += dc[i];
			if ((temp.map[row][col] & direction[i]))
			{
				if (ddr >= 1 && ddr <= 4 && ddc >= 1 && ddc <= 6) {
					if (temp.map[ddr][ddc] & direction[i])
						continue;//前方有墙
					else {
						int k = i; k ^= 2;
						temp.map[row][col] ^= direction[i];//-
						temp.map[ddr][ddc] ^= direction[i];//+
						temp.map[ddr][ddc] ^= direction[k];//-
						temp.map[ddr + dr[i]][ddc + dc[i]] ^= direction[k];//+
					}
				}
				else continue;//有墙且越界
			}
			temp.route[temp.ans++]= rr[i];
			temp.r = ddr; temp.c = ddc;
			q.push(temp);
		}
	}
	
}
int main() {
	while (cin >> col >> row&&col&&row) {
		memset(mp, 0, sizeof(mp));
		for (int i = 1; i <= 4; i++)
			for (int j = 1; j <= 6; j++)
				cin >> mp[i][j];
		bfs();
	}
	return 0;
}

后来用了佚代加深:

#include<iostream>
#include<time.h>
#include<queue>
#include<stdlib.h>
#include<string.h>
using namespace std;
int direction[] = { 1,2,4,8 };
int dr[] = { 0,-1,0,1 };
int dc[] = { -1,0,1,0 };
char rr[] = { 'W','N','E','S' };
int row, col;
int maxd;
int vis[10][10];
int map[10][10];
char route[100];
bool solve(int r,int c,int dep,int dir) {
	if (dep >= maxd)return false;
		for (int i = 0; i < 4; i++) {
			if (dir!=-1&&i == dir)continue;
			int ddr = r, ddc = c;
			ddr += dr[i];
			ddc += dc[i];
			if ((map[r][c] & direction[i]))
			{
				if (ddr >= 1 && ddr <= 4 && ddc >= 1 && ddc <= 6) {
					if (map[ddr][ddc] & direction[i])
						continue;//前方有墙
					else {
						int k = i; k ^= 2;
						map[r][c] ^= direction[i];//-
						map[ddr][ddc] ^= direction[i];//+
						map[ddr][ddc] ^= direction[k];//-
						map[ddr + dr[i]][ddc + dc[i]] ^= direction[k];//+
						route[dep] = rr[i];
						if (solve(ddr, ddc, dep + 1,i^2))return true;
					
						map[r][c] ^= direction[i];//-
						map[ddr][ddc] ^= direction[i];//+
						map[ddr][ddc] ^= direction[k];//-
						map[ddr + dr[i]][ddc + dc[i]] ^= direction[k];//+
					}
				}
				else continue;//有墙且越界
			}
			else {
				if (ddr < 1 || ddr>4 || ddc < 1 || ddc>6) {
					for (int j = 0; j < dep; j++) {
						cout << route[j];
					}
					cout <<rr[i]<< endl;
					return true;
				}
				route[dep] = rr[i];
				if (solve(ddr, ddc, dep + 1, i ^ 2))return true;
			}
	    }
		return false;
}
int main() {
	while (cin >> col >> row&&col&&row) {
		for (int i = 1; i <= 4; i++)
			for (int j = 1; j <= 6; j++)
				cin >> map[i][j];
		for (maxd = 1;; maxd++) {
			if (solve(row,col,0,-1))break;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值