NC232003. Playf and Astringent Map
描述
An Astringent Map can be described as an matrix, which contains only two types of character: 'o' and'.'. 'o' stands for obstacles, and '.' stands for roads that can be passed through. Playf intends to play a catching game with Xu on the Astringent Map.
As we all know, Playf is also a great athlete, she can move at a speed of 2 steps per second, while Xu can only move at a speed of 1 step per second. At each step, Playf and Xu can move freely in any direction of up, down, left, and right, but they cannot move onto obstacles, nor can they walk out of the Astringent Map. Assuming that Playf can catch Xu when they meet at any position, Playf wants to know if it is possible for her to catch Xu.
输入描述
The first line contains two integers n and m () — size of the Astringent Map.
Then, n lines follow. Each line contains m characters, represents the Astringent Map.
It is guaranteed that the Astringent Map only contains 'o', '.', 'P' and 'X', where 'P' represents the initial position of Playf and 'X' represents the initial position of Xu. There is exactly one 'X' and one 'P' on the Astringent Map.
输出描述
If it is possible for Playf to catch Xu, print "Playftql" (without quotes) in a single line.
Otherwise, print "Playftcl" (without quotes) in a single line.
示例1
输入:
3 4 .P.. oooo ..X.
输出:
Playftcl
示例2
输入:
5 2 .. o. P. .o .X
输出:
Playftql
C 解法, 执行用时: 3ms, 内存消耗: 524K, 提交时间: 2022-01-02 15:13:42
#include<stdio.h> #include<string.h> #include<stdlib.h> int n,m; int count=0; char map[100][100]; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; int check(int x,int y){ if(y>=0&&y<m&&x>=0&&x<n&&map[x][y]=='.'||map[x][y]=='P'){ return 1; }return 0; } void dfs(int x,int y){ if(check(x,y)){ map[x][y]='o'; for(int i=0;i<4;i++){ int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(map[xx][yy]=='X'){ count++; return ; }else{ dfs(xx,yy); } } } return ; } int main(void) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%s",map[i]); } int x=0,y=0; int flag=0; for(y=0;y<m;y++){ for(x=0;x<n;x++){ if(map[x][y]=='P'){ flag++; break; } } if(flag)break; } dfs(x,y); if(count)printf("Playftql"); else printf("Playftcl"); return 0; }
C++ 解法, 执行用时: 4ms, 内存消耗: 640K, 提交时间: 2022-01-03 21:30:11
#include<iostream> using namespace std; int n,m,q,w; char a[110][110]; int dx[4]={0,1,-1,0}; int dy[4]={1,0,0,-1}; int nx,ny; void dfs(int x,int y){ a[x][y]='o'; for(int i=0;i<4;i++){ nx=x+dx[i];ny=y+dy[i]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&a[nx][ny]!='o'){ dfs(nx,ny); } } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>a[i][j]; if(a[i][j]=='P'){ q=i;w=j; } } } dfs(q,w); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(a[i][j]=='X'){ cout<<"Playftcl"; return 0; } } } cout<<"Playftql"; return 0; }