[HDU-2612] Find a Way
来源:互联网 发布:php bbs论坛源码 编辑:程序博客网 时间:2024/06/05 02:18
TLE代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<ctype.h>#include<algorithm>#include<queue>#include<vector>using namespace std;int Next[4][2] = { {0,1}, {0,-1}, {1,0}, {-1,0} };struct Pos{ int x, y; Pos(int xx, int yy):x(xx), y(yy) { }};struct Node{ int x, y; int t; Node(int xx, int yy, int tt):x(xx), y(yy), t(tt) { }};const int maxn = 200+10;int n, m;char board[maxn][maxn];bool vis[maxn][maxn];int main() { while( scanf("%d%d", &n, &m)!=-1 ) { int ans = 0x3f3f3f; int cnt=0; int xa, ya, xb, yb; vector<Pos> KFC; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { cin >> board[i][j]; if( board[i][j] == '@' ) { KFC.push_back( Pos(i,j) ); ++cnt; } if( board[i][j] == 'Y' ) { xa = i; ya = j; } if( board[i][j] == 'M' ) { xb = i; yb = j; } } } for(int i=0; i<cnt; i++) { int counter = 0; int sum = 0; memset( vis, 0, sizeof(vis) ); queue<Node>Q; Q.push( Node(KFC[i].x, KFC[i].y, 0) ); vis[ KFC[i].x ][ KFC[i].y ] = 1; bool isfindA = 0, isfindB = 0; while( !Q.empty() ) { Node q = Q.front(); if( q.x==xa && q.y==ya && !isfindA ) { isfindA = 1; sum+=q.t; Q.pop(); continue; } if( q.x==xb && q.y==yb ) { isfindB = 1; sum+=q.t; Q.pop(); continue; } if( isfindA && isfindB ) { break; } for(int i=0; i<4; i++) { int tx = q.x+Next[i][0]; int ty = q.y+Next[i][1]; if( tx<0 || tx>n || ty<0 || ty>m ) { continue; } if( vis[tx][ty] ) { continue; } if( board[tx][ty]=='#' ) { continue; } Q.push( Node(tx,ty,q.t+1) ); vis[tx][ty] = 1; } Q.pop(); } if( isfindA && isfindB ) { ans = min(ans, sum); } } ans *= 11; cout << ans << endl; } return 0;}
AC代码
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<ctype.h>#include<algorithm>#include<queue>#include<vector>using namespace std;struct Pos { int x, y; Pos(int xx, int yy):x(xx), y(yy) { }};struct Node { int x, y; int t; Node(int xx, int yy, int tt):x(xx), y(yy), t(tt) { }};const int maxn = 200+10;int Next[4][2] = { {0,1}, {0,-1}, {1,0}, {-1,0} };char board[maxn][maxn];int sum[maxn][maxn];int isfind[maxn][maxn];bool vis[maxn][maxn];int n, m;void DFS(int x, int y) { memset( vis, 0, sizeof(vis) ); queue<Node> Q; Q.push( Node(x,y,0) ); vis[x][y] = 1; while( !Q.empty() ) { Node q = Q.front(); if( board[q.x][q.y] == '@' ) { isfind[q.x][q.y]++; sum[q.x][q.y] += q.t; } for(int i=0; i<4; i++) { int tx = q.x+Next[i][0]; int ty = q.y+Next[i][1]; if( tx<0 || tx>n || ty<0 || ty>m ) { continue; } if( vis[tx][ty] ) { continue; } if( board[tx][ty] == '#' ) { continue; } if( board[tx][ty] == 'Y' || board[tx][ty] == 'M' ) { continue; } Q.push( Node(tx,ty,q.t+1) ); vis[tx][ty] = 1; } Q.pop(); }}int main() { while( scanf("%d %d", &n, &m)!=EOF ) { int cnt = 0; int xa, ya, xb, yb; vector<Pos> KFC; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { cin >> board[i][j]; if( board[i][j] == '@' ) { KFC.push_back( Pos(i,j) ); ++cnt; } if( board[i][j] == 'Y' ) { xa = i; ya = j; } if( board[i][j] == 'M' ) { xb = i; yb = j; } } } memset(sum, 0, sizeof(sum)); memset(isfind, 0, sizeof(isfind)); DFS(xa, ya); DFS(xb, yb); int ans = 0x3f3f3f; for(int i=0; i<cnt; i++) { if( isfind[KFC[i].x][KFC[i].y] == 2 ) { ans = min( ans, sum[KFC[i].x][KFC[i].y] ); } } ans *= 11; cout << ans << endl; } return 0;}
阅读全文
0 0
- HDU 2612 Find a way
- HDU-2612-Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU 2612 Find A Way
- hdu 2612 Find a way
- HDU-2612-Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU-2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- EasyPusher 华为手机硬编码 [OMX.IMG.TOPAZ.Encoder] failed to set input port definition parameters.
- 【FZU
- 2697: 特技飞行
- SiteMesh3简介及使用
- Android MVP 详解(上)
- [HDU-2612] Find a Way
- caffe 参数的详解solver文件<一>
- C#中如何使用水晶报表
- Windows 7下用VMware Workstation 10虚拟机安装 Ubuntu 14.04
- 如何尽可能麻烦的通过open cv控制一个小灯
- java 驼峰字符和下划线字符相互转换工具类
- java中的native方法
- 网络的概念 2
- 马尔科夫链的一个应用实例