UVA 705 - Slash Maze
来源:互联网 发布:mac命令行自动补全 编辑:程序博客网 时间:2024/05/21 13:58
题目不难,但是有点繁琐,需要仔细分析处理。
要点:将/\对应的图转换为一般迷宫问题,可以将整个图顺时针选择45度
坐标对应的法则:不管是‘/’ 还是 ‘\’ 只关注其中点的坐标即可
令x(i,j)表示第i行第j列的slash的中点x坐标
y(i,j)表示第i行第j列的slash的中点y坐标
则 x(i , j + 1) = x(i , j) + 1;
y(i , j + 1) = y(i , j) + 1;
x(i+1 , j) = x(i , j) + 1;
y(i+1 , j) =y(i , j) - 1;
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int size = 100 + 110;char s[size][size];int pos[size][size][2];int u[size * size][4]; //第一维对应 row * nw + col ,第二维对应上下左右int d[][2] = { {-1 , -1} , {-1 , 0} , {0 , -1} , {0, 0}};int v[size*size];int nw , nh;int dfs(int x , int y , int len){v[x] = 1;for(int i = 0 ; i < 4 ; ++i){if(u[x][i] < 0) continue;if(v[u[x][i]]) continue;int r = dfs(u[x][i] , y , len + 1);if(r) return 1 + r;else{u[x][0] = u[x][1] = u[x][2] = u[x][3] = -1;return 0;}}int a = 0 , b = 0;for(int i = 0 ; i < 4 ; ++i){if(u[x][i]>=0 && u[u[x][i]][0] == -1) a = 1;if(u[x][i] == -1) b = 1;}if(!a && !b) return 1;else{u[x][0] = u[x][1] = u[x][2] = u[x][3] = -1;return 0;} }int d2[][2] = {{-1 , 0} , { 1 , 0} , {0 , -1} , {0 , 1}};void f(int x , int y){if(v[x*nw + y]) return;v[x*nw + y] = 1;int t = x * nw + y;for(int i = 0 ; i < 4; ++i){int tx = d2[i][0] + x;int ty = d2[i][1] + y;if(tx < 0 || ty < 0) continue;if(tx >= nh || ty >= nw) continue;u[t][i] = tx * nw + ty;}}int main(){int w , h;int nloop = 1;while(cin >> w >> h && w && h){for(int i = 0 ; i < h ; ++i)cin >> s[i];pos[0][0][0] = pos[0][0][1] = 0;for(int i = 1; i < w ; ++i){pos[0][i][0] = pos[0][i-1][0] + 1;pos[0][i][1] = pos[0][i-1][1] + 1;}for(int i = 1 ; i < h ; ++i){for(int j = 0 ; j < w ; ++j){pos[i][j][0] = pos[i-1][j][0] + 1;pos[i][j][1] = pos[i-1][j][1] - 1;}}int minh , maxh;int minw , maxw;if(s[0][0] == '/') minh = 0;else minh = -1;if(s[h-1][w-1] == '/') maxh = pos[h-1][w-1][0];else maxh = pos[h-1][w-1][0] + 1;if(s[0][w-1] == '/') maxw = pos[0][w-1][1] + 1;else maxw = pos[0][w-1][1];if(s[h-1][0] == '/') minw = pos[h-1][0][1] - 1;else minw = pos[h-1][0][1];nh = maxh - minh;nw = maxw - minw;memset(u , -1 , sizeof(u));memset(v , 0 , sizeof(v));for(int i = 0 ; i < h ; ++i){for(int j = 0 ; j < w ; ++j){int x = pos[i][j][0] - minh;int y = pos[i][j][1] - minw;pos[i][j][0] = x;pos[i][j][1] = y;if(s[i][j] == '/'){for(int k = 0 ; k < 4 ; ++k){int tx = x + d[k][0];int ty = y + d[k][1];if(tx < 0 || ty < 0) continue;if(tx >= nh || ty >= nw) continue;f(tx , ty);int t = tx * nw + ty;u[t][1 - (k > 1)] = -2;}}else{for(int k = 0 ; k < 4 ; ++k){int tx = x + d[k][0];int ty = y + d[k][1];if(tx < 0 || ty < 0) continue;if(tx >= nh || ty >= nw) continue;f(tx ,ty);int t = tx * nw + ty;u[t][3 - k % 2] = -2;}}}}int m = -1 , cnt = 0;memset(v , 0 , sizeof(v));for(int i = 0 ; i < nh ; ++i){for(int j = 0 ; j < nw ; ++j){int t = i * nw + j;if(!v[t]){int r = dfs(t , t , 0);if(r) { ++cnt , m = m < r ? r : m; } }}}cout << "Maze #" << nloop++ << ":" << endl;if(cnt) cout << cnt << " Cycles; the longest has length " << m << "." << endl << endl;else cout << "There are no cycles." << endl << endl;}return 0;}
0 0
- uva 705 - Slash Maze
- uva 705 - Slash Maze
- uva 705 - Slash Maze
- uva 705 - Slash Maze
- UVa 705 - Slash Maze
- UVA 705 - Slash Maze
- uva 705Slash Maze
- UVA 705 Slash Maze
- uva 705 Slash Maze
- Slash Maze(uva 705)
- uva 705 - Slash Maze
- UVa 705 - Slash Maze
- UVA 705 Slash Maze
- uva 705 Slash Maze
- UVA 705 Slash Maze
- UVA - 705 Slash Maze
- UVA 705 - Slash Maze
- UVA - 705 Slash Maze
- VS2008制作安装程序
- VC++ 编译libcurl 支持SSL,GZIP
- JavaScript Object的key
- 【Oracle】【FAQ】Oracle 12c使用sqlplus登录SQL界面,出现“ORA-12560: TNS: 协议适配器错误”
- 创建cocos2dx项目
- UVA 705 - Slash Maze
- Hibernate注解详解
- warning MSB8003: Could not find WindowsSDKDir variable from the registry
- writeObject可以写n个,但是readObject()却只能读一次,你做一下测试。 如果你需要序列化好几个类的话,建议你用json;或者自己写两个方法,一个是将对象转换为字符串,一个是将字符串
- 去哪儿网2015校园招聘前端笔试题
- IP数据报格式
- 视频编解码学习之一:介绍、数字视频、信息论基础
- volatile语义
- (尚桂谷) ORCL大小写的控制函数 ,字符控制函数,数字函数,月期函数