跳到正文

题解

本蒟蒻的第一篇题解

yujincheng约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
题目
P7185

版本

  1. v2当前
  2. v1

本蒟蒻的第一篇水题题解,请大佬多多指出错误。 代码很长,请不要介意。

题目大意

给出 n×mn \times m 的字符地图(题目为 rr 与 cc ),在地图中, ′M′'M' 与 ′Z′'Z' 是终点与起点,

C++
#include <bits/stdc++.h>
using namespace std;
int n,m,ansx,ansy;
char c[30][30];
bool u,d,l,r;
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>c[i][j];
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(c[i][j]=='.')
				continue;
			if(c[i][j]=='|')
			{
				if(c[i-1][j]=='.')
				{
					ansx=i-1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i+1][j]=='.')
				{
					ansx=i+1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='-')
			{
				if(c[i][j-1]=='.')
				{
					ansx=i,ansy=j-1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i][j+1]=='.')
				{
					ansx=i,ansy=j+1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='+')
			{
				if(c[i][j-1]=='.')
				{
					ansx=i,ansy=j-1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i][j+1]=='.')
				{
					ansx=i,ansy=j+1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i-1][j]=='.')
				{
					ansx=i-1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i+1][j]=='.')
				{
					ansx=i+1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='1')
			{
				if(c[i][j+1]=='.')
				{
					ansx=i,ansy=j+1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i+1][j]=='.')
				{
					ansx=i+1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='2')
			{
				if(c[i][j+1]=='.')
				{
					ansx=i,ansy=j+1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i-1][j]=='.')
				{
					ansx=i-1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='3')
			{
				if(c[i-1][j]=='.')
				{
					ansx=i-1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i][j-1]=='.')
				{
					ansx=i,ansy=j-1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
			else if(c[i][j]=='4')
			{
				if(c[i][j-1]=='.')
				{
					ansx=i,ansy=j-1;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
				else if(c[i+1][j]=='.')
				{
					ansx=i+1,ansy=j;
					cout<<ansx<<" "<<ansy;
					goto end;
				}
			}
		}
	end:;
	//上面是否联通
	if(c[ansx-1][ansy]=='.')
		u=0;
	else if(c[ansx-1][ansy]=='|'||c[ansx-1][ansy]=='+'||c[ansx-1][ansy]=='1'||c[ansx-1][ansy]=='4')
		u=1;
	else
		u=0;
	//下面是否联通
	if(c[ansx+1][ansy]=='.')
		d=0;
	else if(c[ansx+1][ansy]=='+'||c[ansx+1][ansy]=='|'||c[ansx+1][ansy]=='2'||c[ansx+1][ansy]=='3')
		d=1;
	else
		d=0;
	//左边是否联通
	if(c[ansx][ansy-1]=='.')
		l=0;
	else if(c[ansx][ansy-1]=='+'||c[ansx][ansy-1]=='-'||c[ansx][ansy-1]=='1'||c[ansx][ansy-1]=='2')
		l=1;
	else
		l=0;
	//右边是否联通
	if(c[ansx][ansy+1]=='.')
		r=0;
	else if(c[ansx][ansy+1]=='+'||c[ansx][ansy+1]=='-'||c[ansx][ansy+1]=='3'||c[ansx][ansy+1]=='4')
		r=1;
	else
		r=0;
	//以下不用讲了吧
	if(d&&u&&l&&r)
		cout<<" +";
	else if(d&&u)
		cout<<" |";
	else if(l&&r)
		cout<<" -";
	else if(d&&r)
		cout<<" 1";
	else if(u&&r)
		cout<<" 2";
	else if(u&&l)
		cout<<" 3";
	else if(d&&l)
		cout<<" 4";
	return 0;
}
原文