lightoj1426 Blind Escape

思路:有一个r*c的grid,里面有两种字符,'#'表示墙,不可穿过,'.'表示free,可穿过,现在在这个里面有一个盲人,他可以向东南西北四个方向走,需要靠你给的指令走出来,如果不能走出来就是Impossible,不然的话输出最短且字典序最小的命令串。

显然是搜索,当然不能走出来最好判断,每个点搜一遍就好了,最后判断下。问题就是可以走出来的时候怎么搜。

首先是queue中的状态是啥,这里的话应该是点集而不是单个的点,因为题目要求不管人在那个点根据这个指令都是可以走出来的,所以每对于一群点要同时移动它,到达下一个点集状态,判断这个状态是否出现过没有,没有的话就是上一个状态到这个状态+方向字符;最后出来的条件就是这个状态中不含有任何grid中的点,这个就是最后的答案。

这里涉及到状态的hash,将其hash成string,点的话可以自定义(重载<符号),也可以用pair。

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef vector<ii> vec;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int D = 4, N = 16;
const int dx[] = {0,-1,1,0};
const int dy[] = {1,0,0,-1};
const char dc[] = {'E','N','S','W'};
vec s,u,v;
queue<vec> que;
map<vec,string> mp;
char a[N][N];
bool vis[N][N];
int r, c, nxtx[N][N][4], nxty[N][N][4];
/*erase(first,last);删除从first到last之间的字符串,first,last是迭代器*/
void MinRepresent(vec& ans){
	sort(ans.begin(),ans.end());
	ans.erase(unique(ans.begin(),ans.end()),ans.end());
}
bool InBounds(int x,int y){
	return 1 <= x && x <= r && 1 <= y && y <= c;
}
void Read(){
	scanf("%d%d",&r,&c);
	for (int i = 1;i <= r;++i)
		scanf("%s",a[i] + 1);
	return ;
}
void Initation(){
	s.clear();
	for (int i = 1;i <= r;++i){
		for (int j = 1;j <= c;++j){
			if (a[i][j] == '.') s.push_back(ii(i,j));
			for (int d = 0;d < 4;++d){
				if (a[i][j] == '.'){
					int& nx = nxtx[i][j][d];
					int& ny = nxty[i][j][d];
					for (nx = i + dx[d],ny = j + dy[d];InBounds(nx,ny) && a[nx][ny] == '.';nx += dx[d],ny += dy[d]){}
					if (InBounds(nx,ny)){
						nx -= dx[d];
						ny -= dy[d];
					}					
				}
			}
		}
	}
	return ;
}
bool dfs(int x,int y){
	if (!InBounds(x,y)) return true;
	if (vis[x][y]) return false;
	vis[x][y] = true;
	for (int i = 0;i < 4;++i){
		if (dfs(nxtx[x][y][i],nxty[x][y][i])) return true;
	}
	return false;
}
bool Impossible(){
	for (int i = 1;i <= r;++i){
		for (int j = 1;j <= c;++j){
			if (a[i][j] == '.'){
				memset(vis, false,sizeof vis);
				if (!dfs(i,j)) return true;//(i,j) can not escape out of grid.
			}
		}
	}
	return false;
}
string BFS(){
	mp.clear();
	mp[s] = "";
	while(!que.empty()) que.pop();
	que.push(s);
	while(!que.empty()){
		u = que.front();
		que.pop();
		int size = (int)(u.size());
		if (size == 0) return mp[u];
		for (int i = 0;i < 4;++i){
			v.clear();
			for (int j = 0;j < size;++j){
				int first = u[j].first;
				int second = u[j].second;
				int nx = nxtx[first][second][i];
				int ny = nxty[first][second][i];
				if (InBounds(nx,ny)) v.push_back(ii(nx,ny));
			}
			MinRepresent(v);
			if (mp.find(v) == mp.end()){
				mp[v] = mp[u] + dc[i];
				que.push(v);
			}
		}
	}
	return "";
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	scanf("%d",&t);
	while(t--){
		Read();
		Initation();
		printf("Case %d: ", ++icase);
		if (Impossible()) cout << "Impossible\n";
		else{
			cout << BFS() << endl;
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值