NC216218. 逃出机房
描述
输入描述
第一行给出n,m(2<=n,m<=10)。代表n*m的矩阵。
后面给出n行m列的矩阵。矩阵中'*'表示道路,'@'表示门口,一个机房有两扇门,'#'表示桌子,'z'代表zyj,'H'代表hl。
输出描述
如果hl能抓住zyj,则输出一行“give me northeast chicken rice and milk tea!”(不带引号)
否则输出一行“give me northeast chicken rice and milk tea TOMORROW!”(不带引号)
示例1
输入:
2 2 HZ @@
输出:
give me northeast chicken rice and milk tea TOMORROW!
示例2
输入:
2 3 Z@H #@#
输出:
give me northeast chicken rice and milk tea!
说明:
hl在门口堵住了zyj示例3
输入:
2 3 Z*H #@@
输出:
give me northeast chicken rice and milk tea!
C(clang11) 解法, 执行用时: 3ms, 内存消耗: 384K, 提交时间: 2021-01-29 18:49:16
#include<stdio.h> int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; int min=9999; int x0,y0; char a[11][11]; int b[11][11]={0}; int x,y; void duqi(int x1,int y1,int k){ int f; if(x1==x&&y1==y){ if(k<min) min=k; return ; } for(f=0;f<4;f++){ x0=x1+dx[f]; y0=y1+dy[f]; if(b[y0][x0]==0&&(a[y0][x0]=='*'||a[y0][x0]=='Z'||a[y0][x0]=='H'||a[y0][x0]=='@')) { b[y0][x0]=1; duqi(x0,y0,k+1); b[y0][x0]=0; } } return ; } int main() { int i,j,m,n,x3,y3,x2,y2,h,g; int w1,w2; scanf("%d%d\n",&m,&n); for(i=0;i<m;i++) gets(a[i]); for(i=0;i<m;i++) for(j=0;j<n;j++){ if(a[i][j]=='Z'){ x3=j;y3=i; } if(a[i][j]=='H'){ x2=j;y2=i;} } for(i=0;i<m;i++) for(j=0;j<n;j++) if(a[i][j]=='@'){ x=j;y=i; for(h=0;h<m;h++) for(g=0;g<n;g++) b[h][g]=0; b[y3][x3]=1; duqi(x3,y3,0); b[y3][x3]=0; w1=min;min=9999; for(h=0;h<m;h++) for(g=0;g<n;g++) b[h][g]=0; b[y2][x2]=1; duqi(x2,y2,0); b[y2][x2]=0; w2=min;min=9999; if(w1<w2) { printf("give me northeast chicken rice and milk tea TOMORROW!"); return 0; } } printf("give me northeast chicken rice and milk tea!"); }
C++(clang++11) 解法, 执行用时: 6ms, 内存消耗: 376K, 提交时间: 2021-01-01 19:20:35
#include<bits/stdc++.h> using namespace std; int n, m, dis[123][123], xz, yz, xh, yh, dir[][2] = {0, 1, 0, -1, -1, 0, 1, 0}; string s[123]; typedef pair<int, int>pii; queue<pii>q; int main() { cin >> n >> m; for(int i = 0; i < n; i++) { cin >> s[i]; for(int j = 0; s[i][j]; j++) if(s[i][j] == 'H') xh = i, yh = j; else if(s[i][j] == 'Z') xz = i, yz = j; } dis[xz][yz] = 2000; dis[xh][yh] = 1000; q.push(pii(xh, yh)); q.push(pii(xz, yz)); while(!q.empty()) { pii cur = q.front(); q.pop(); int curx = cur.first, cury = cur.second; for(int i = 0; i < 4; i++) { int newx = curx + dir[i][0]; int newy = cury + dir[i][1]; if(newx < 0 || newy < 0 || newx >= n || newy >= m) continue; if(!dis[newx][newy] && s[newx][newy] != '#') { dis[newx][newy] = dis[curx][cury] + 1; if(s[newx][newy] == '@' && dis[newx][newy] >= 2000) return cout << "give me northeast chicken rice and milk tea TOMORROW!", 0; } } } cout << "give me northeast chicken rice and milk tea!"; }