【例题 7-9 UVA-1601】The Morning after Halloween
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
对于没有出现的,当成0节点就好。
所以总是认为有3个人需要走到各自的终点。
将平面图转成点边图。这样比较好枚举。
(二维变成一维,模拟的时候变量都少了一半啦)
然后每次按照要求模拟走一下就好。
(三重循环,枚举每一个人下一步走到了哪个位置即可
(注意两个0节点可以走到相同的位置->因为实际上他们都不存在);
然后用一个三元组加入队列里。
f[x][y][z]来判重就好了。
(直接用map来判重会TLE到死。。
【代码】
/*
1.Shoud it use long long ?
2.Have you ever test several sample(at least therr) yourself?
3.Can you promise that the solution is right? At least,the main ideal
4.use the puts("") or putchar() or printf and such things?
5.init the used array or any value?
6.use error MAX_VALUE?
7.use scanf instead of cin/cout?
8.whatch out the detail input require
*/
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,pair<int,int> > piii;
const int N = 256;
const int NN = 16;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n,m,tot;
int v[3][2];
char s[NN+5][NN+5];
vector <int> g[N+10];
int dic[N+10][N+10][N+10];
queue <piii> dl;
bool jue(int x,int y){
if (x==0 || y==0) return false;
if (x==y) return true;
return false;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("F:\\c++source\\rush_in.txt", "r", stdin);
freopen("F:\\c++source\\rush_out.txt", "w", stdout);
#endif
ios::sync_with_stdio(0),cin.tie(0);
while (cin >> m>> n >> tot){
if (n==0 && m==0 && tot==0) break;
cin.get();
memset(v,0,sizeof v);
for (int i = 0;i <=N;i++) g[i].clear();
for (int i = 0;i <= N;i++) g[i].push_back(i);
for (int i = 1;i <= n;i++) cin.getline(s[i]+1,N+10);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
if (s[i][j]!='#'){
for (int k = 0;k < 4;k++){
int ti = i + dx[k],tj = j + dy[k];
if (ti >=1 && ti <= n && tj>=1 && tj<=m && s[ti][tj]!='#'){
int x = (i-1)*m+j,y = (ti-1)*m+tj;
g[x].push_back(y);
}
}
for (int k = 0;k < tot;k++){
if (s[i][j]==('a'+k)) v[k][0] = (i-1)*m+j;
if (s[i][j]==('A'+k)) v[k][1] = (i-1)*m+j;
}
}
memset(dic,-1,sizeof dic);
dic[v[0][0]][v[1][0]][v[2][0]] = 0;
piii temp = make_pair(v[0][0],make_pair(v[1][0],v[2][0]));
dl.push(temp);
while (!dl.empty()){
temp = dl.front();
dl.pop();
int x = temp.first,y = temp.second.first,z = temp.second.second;
for (int i = 0;i < (int)g[x].size();i++)
for (int j = 0;j < (int)g[y].size();j++)
for (int k = 0;k < (int)g[z].size();k++){
int tx = g[x][i],ty = g[y][j],tz = g[z][k];
if (jue(tx,ty) || jue(tx,tz) || jue(ty,tz)) continue;
if (!(x==0||y==0)&&x == ty && tx == y) continue;
if (!(x==0||z==0)&&x == tz && tx == z) continue;
if (!(y==0||z==0)&&y == tz && ty == z) continue;
piii temp1 = make_pair(tx,make_pair(ty,tz));
if (dic[tx][ty][tz]==-1){
dic[tx][ty][tz] = dic[x][y][z] + 1;
dl.push(temp1);
}
}
}
cout << dic[v[0][1]][v[1][1]][v[2][1]] <<endl;
}
return 0;
}
【例题 7-9 UVA-1601】The Morning after Halloween的更多相关文章
- UVA 1601 The Morning after Halloween
题意: 给出一个最大为16×16的迷宫图和至多3个ghost的起始位置和目标位置,求最少经过几轮移动可以使三个ghost都到达目标位置.每轮移动中,每个ghost可以走一步,也可以原地不动,需要注意的 ...
- UVA - 1601 The Morning after Halloween (BFS/双向BFS/A*)
题目链接 挺有意思但是代码巨恶心的一道最短路搜索题. 因为图中的结点太多,应当首先考虑把隐式图转化成显式图,即对地图中可以相互连通的点之间连边,建立一个新图(由于每步不需要每个鬼都移动,所以每个点需要 ...
- UVA - 1601 The Morning after Halloween (双向BFS&单向BFS)
题目: w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
- <<操作,&0xff以及|的巧妙运用(以POJ3523---The Morning after Halloween(UVa 1601)为例)
<<表示左移,如a<<1表示将a的二进制左移一位,加一个0,&0xff表示取最后8个字节,如a&0xff表示取a表示的二进制中最后8个数字组成一个新的二进制数, ...
- [uva P1601] The Morning after Halloween
[uva P1601] The Morning after Halloween 题目链接 非常经典的一道题目,lrj的书上也有(貌似是紫书?). 其实这题看起来就比较麻烦.. 首先要保证小鬼不能相遇, ...
- UVa 1601 万圣节后的早晨
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【UVa】1601 The Morning after Halloween(双向bfs)
题目 题目 分析 双向bfs,对着书打的,我还调了好久. 代码 #include<cstdio> #include<cstring> #include<c ...
- uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...
- 【Uva 1601】The Morning after Halloween
[Link]: [Description] 给你一张平面图; 最多可能有3只鬼; 给出这几只鬼的初始位置; 然后,这几只鬼有各自的终点; 每秒钟,这几只鬼能同时移动到相邻的4个格子中的一个 任意两只鬼 ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
随机推荐
- LeetCode 0、《两数相加》
一.给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 ...
- noip 2018 day1 T3 赛道修建 贪心_树上问题_multiset
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> using namespace std; #define maxn 5000 ...
- react-native signatures do not match the previously installed version;
原因:手机上已经安装过打包后的apk应用,与真机调试无法共存. 解决办法:删除手机上已经安装过的apk应用.
- Vim操作的四种模式
Vim的四种模式一.启动Vim1.双击桌面的图标,就可以启动Vim(是图形界面的)2.在开始菜单---点--运行 接着输入 vim 或者gvim,就可以启动Vim或Gvim了.二.Vim的模式1.Vi ...
- 安卓https
http://www.tuicool.com/articles/NrmE3e http://blog.csdn.net/guestcode/article/details/50194357 http: ...
- 转:IOS的推送。是一个强大的功能
IOS下我们很多人天天开着 邮件推送 微信推送 QQ推送 微博推送 新浪微博推送,安卓敢吗? 五个后台进程消耗电.流量谁敢这么做?现在安卓也出了推送,但绝对是伪推送.实际是挂了个进程,关掉了就收不到. ...
- Can't access RabbitMQ web management interface after fresh install
http://stackoverflow.com/questions/22850546/cant-access-rabbitmq-web-management-interface-after-fres ...
- VMware Vsphere 6.0安装部署 总体部署架构
(一)总体部署架构 本教程用于学习目的,力求详尽的介绍安装部署过程和各组件之间的关系,部署过程从最简单的模型开始,系列文章按时间顺序依次展开,每篇介绍一个组件. 开始阶段,按照一台物理服务器,部署所有 ...
- python2 python3 m2crypto 安装(rsa 私钥文件加密)
转自作者:大道至简_Andy 原文链接:https://www.jianshu.com/p/b308357ef649 第一种方式:使用apt-get(以Python2版本进行测试的) sudo apt ...
- 【hihocoder 1369】网络流一·Ford-Fulkerson算法
[Link]:http://hihocoder.com/problemset/problem/1369 [Description] [Solution] 最大流模板题 [NumberOf WA] [R ...