浅参考一下吧
2023-04-13 14:50:59
发布于:浙江
50阅读
0回复
0点赞
解题思路详见代码里的注释
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
//输入迷宫地图
char a[n][m];
for (int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin >> a[i][j];
}
}
string s;
cin >> s;
int x=0,y=0;//横纵坐标初始值
for(int i=0;i<s.size();i++){
char c = s[i];
int nx = x, ny = y;//狗走到哪个坐标
if (c == 'T') {nx--; }
else if (c == 'D') {nx++;}
else if (c == 'L') {ny--;}
else if (c == 'R') {ny++;}
if (nx < 0 || nx >= n || ny < 0 || ny >= m) {cout << "NO Way!" << endl;}
else {
x = nx;
y = ny;
if (a[x][y] == 'X') {cout << "I Can do win!" << endl; }
else {cout << "I Can" << endl;}}
}
return 0;
}
这里空空如也
有帮助,赞一个