UDP广播协议叫吃饭


私信TA

用户名:Mustenaka

访问量:135533

签 名:

个人博客www.mustenaka.cn

等  级
排  名 12
经  验 23796
参赛次数 8
文章发表 196
年  龄 3
在职情况 学生
学  校 Sky_box
专  业 NE

  自我简介:

欢迎光临我的博客www.mustenaka.cn,Python,C#,U3D,C/C++开发合作可以找我

解题思路:

参考代码:

#include<bits/stdc++.h>
#define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int maxn=105;
int t,n,q;
int x,y,pos_x,pos_y;
char mmp[maxn][maxn];
string cmd;

int main() {
	//hh;
	cin>>t;
	while(t--) {
		memset(mmp,0,sizeof(mmp));
		cin>>n;
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=n; j++) {
				cin>>mmp[i][j];
				if(mmp[i][j]=='S') {
					x=i,y=j;
					pos_x=i,pos_y=j;
				}
			}
		}
		cin>>q;
		while(q--) {
			cin>>cmd;
			x=pos_x,y=pos_y;
			for(int i=0; i<cmd.length(); i++) {
				if(cmd[i]=='L') {
					y--;
				} else if(cmd[i]=='R') {
					y++;
				} else if(cmd[i]=='U') {
					x--;
				} else if(cmd[i]=='D') {
					x++;
				}

				if(mmp[x][y]=='T') {
					cout<<"I get there!"<<endl;
					break;
				} else if(mmp[x][y]!='T'&&i==cmd.length()-1&&(x>=1&&x<=n&&y>=1&&y<=n)) {
					cout<<"I have no idea!"<<endl;
					break;
				} else if(mmp[x][y]=='#') {
					cout<<"I am dizzy!"<<endl;
					break;
				} else if(x<1||x>n||y<1||y>n) {
					cout<<"I am out!"<<endl;
					break;
				}
			}
			cmd.clear();
		}
	}
	return 0;
}
//这份代码错误50%

然后我看了一下别人的题解,进行了一下修改,吧else if换成了if进行判断,就对了

#include<bits/stdc++.h>
#define hh ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int maxn=105;
int t,n,q;
int x,y,pos_x,pos_y;
char mmp[maxn][maxn];

int main() {
	//hh;
	cin>>t;
	while(t--) {
		memset(mmp,0,sizeof(mmp));
		cin>>n;
		for(int i=1; i<=n; i++) {
			for(int j=1; j<=n; j++) {
				cin>>mmp[i][j];
				if(mmp[i][j]=='S') {
					x=i,y=j;
					pos_x=i,pos_y=j;
				}
			}
		}
		cin>>q;
		while(q--) {
			string cmd;
			cin>>cmd;
			x=pos_x,y=pos_y;
			for(int i=0; i<cmd.length(); i++) {
				if(cmd[i]=='L')
					y--;
				if(cmd[i]=='R')
					y++;
				if(cmd[i]=='U')
					x--;
				if(cmd[i]=='D')
					x++;
					
				if(mmp[x][y]=='T') {
					cout<<"I get there!"<<endl;
					break;
				} else if(mmp[x][y]=='#') {
					cout<<"I am dizzy!"<<endl;
					break;
				} else if(mmp[x][y]==0) {
					cout<<"I am out!"<<endl;
					break;
				}
				
				if(mmp[x][y]!='T'&&i==cmd.length()-1&&mmp[x][y]!=0) {
					cout<<"I have no idea!"<<endl;
					break;
				}
			}
		}
	}
	return 0;
}

AC

 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区