The Morning after Halloween UVA
来源:互联网 发布:cmd java 命令行参数 编辑:程序博客网 时间:2024/06/05 11:39
状态空间搜索
题目描述:把图中的小写字母,移动到对应的大写字母处,这些小写字母可以同时移动,而且只能通过空白处移动,两个字母之间不可以互换位置,也不可以在同一个位置。
解题分析:直接暴力bfs的话,状态数目是256*256*256,状态数目不是很多,但是每一个状态的转移需要5*5*5的次数,这样的话时间复杂度就大了去了。注意到,题目中并不全是空白处,而是充满了大量的墙壁,所以以对所有的空白处建图,在新建的图上bfs,就能省很多时间,问题变成了如何建图:我是给每一个空白的点重新赋个id,建立无向图。在本题中n < 3,在状态转移的时候,如果n = 1,需要一重循环,n = 2,需要二重循环,n = 3需要三重循环,当然你暴力枚举这三种情况也可以。如何统一的处理这三种情况呢,我是直接设立三个坐标,如果某个坐标不存在,那这个坐标在bfs的过程中就永远是
初始的那个值不会变,而且不会增加循环的次数。还有就是判重的问题了,刚开始不敢开三维数组,怕MLE,结果交上直接过了,所以就直接开了个三维数组判重,如果不开三维数组的话,就需要hash判重了,当然空间花费也不小。
代码如下:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <set>#include <queue>using namespace std;const int dr[] = {0,-1,0,1,0};const int dc[] = {0,0,1,0,-1};const int maxn = 20;struct Node{ int loc[3]; int dist; Node(int a = 0,int b = 0,int c = 0,int d = 0) { loc[0] = a; loc[1] = b; loc[2] = c; dist = d; }};char graph[maxn][maxn];int id[maxn][maxn];vector<int> g[maxn*maxn];Node st,ed;int w,h,n,cnt;int visit[16*16][16*16][16*16];void init(){ memset(visit,0,sizeof(visit)); memset(id,0,sizeof(id)); for(int i = 0; i < maxn*maxn; i++) g[i].clear(); for(int i = 0; i < 3; i++)//先给起点和终点赋一个初始的值,并且保证这个初始的值和后面的点的id不冲突 { st.loc[i] = ed.loc[i] = i; } for(int i = 0; i < h; i++) { gets(graph[i]); } cnt = 3; for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(graph[i][j] != '#') { id[i][j] = cnt++; } } } for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(id[i][j]) { int u = id[i][j]; if(graph[i][j] >= 'a' && graph[i][j] <= 'z') { int x = graph[i][j] - 'a'; st.loc[x] = u; } else if(graph[i][j] >= 'A' && graph[i][j] <= 'Z') { int x = graph[i][j] - 'A'; ed.loc[x] = u; } for(int k = 0; k < 5; k++) { int x = i + dr[k]; int y = j + dc[k]; if(x >= 0 &&x < h && y >= 0 && y < w && id[x][y]) { g[u].push_back(id[x][y]); } } } } } for(int i = 0; i < 3; i++) { if(st.loc[i] == i)//如果n < 3 { if(g[i].size() == 0) g[i].push_back(i); } }}bool judge(Node &t){ for(int i = 0; i < 3; i++) { if(t.loc[i] != ed.loc[i]) return false; } return true;}int solve(){ queue<Node> q; st.dist = 0; q.push(st); visit[st.loc[0]][st.loc[1]][st.loc[2]] = 1; while(!q.empty()) { Node t = q.front(); q.pop(); if(judge(t)) return t.dist; for(int i = 0; i < g[t.loc[0]].size(); i++) { int a = g[t.loc[0]][i]; for(int j = 0; j < g[t.loc[1]].size(); j++) { int b = g[t.loc[1]][j]; if(a == t.loc[1] && b == t.loc[0]) continue;//a和b互换位置是不允许的 if(a == b) continue;//a和b在同一个位置也是不允许 for(int k = 0; k < g[t.loc[2]].size(); k++) { int c = g[t.loc[2]][k]; if(a == t.loc[2] && c == t.loc[0]) continue;//同上 if(b == t.loc[2] && c == t.loc[1]) continue; if(a == c || b == c) continue; if(!visit[a][b][c]) { visit[a][b][c] = 1; q.push(Node(a,b,c,t.dist + 1)); } } } } }}int main(){ while(scanf("%d %d %d",&w,&h,&n) == 3 && (w || h || n)) { getchar(); init(); printf("%d\n",solve()); } return 0;}
阅读全文
0 0
- The Morning after Halloween UVA
- The Morning after Halloween UVA
- 隐式图--UVA - 1601 The Morning after Halloween
- UVa OJ 1601 - The Morning after Halloween
- UVA 1601 The Morning after Halloween [DBFS]
- uva 1601 The Morning after Halloween
- uva 1601 The Morning after Halloween code2
- 1601 - The Morning after Halloween
- 1601 - The Morning after Halloween
- 1601 - The Morning after Halloween
- UVa1601-The Morning after Halloween
- UVA1601 The Morning after Halloween
- The Morning after Halloween(POJ
- UVa #1601 The Morning after Halloween (例题7-9)
- UVa 1601 - The Morning after Halloween(单向BFS版)
- UVa 1601 - The Morning after Halloween(双向BFS版)
- UVa 1601:The Morning after Halloween(BFS)
- UVa 1601 The morning after Halloween(单向BFS+双向BFS)
- 实验二(HTML以及J2EE简单编程)
- DDR3布局布线的一些注意事项
- 【SQL解惑】谜题6:预订旅馆房间(待完善)
- css3中的flex
- Java语言brea使用方法
- The Morning after Halloween UVA
- Contestants Division (树形dp)
- 【小白】selenium入门(二)学习笔记+登录百度
- Circuit:简单电路实验
- Icecream Screen Recorder Pro(高清视频录制工具)官方破解版V5.02下载 |含icecream screen recorder pro破解补丁
- c# des加密与java同步
- 树莓派-pypi-GPIO
- Intellij插件
- mac/linux常用命令之date