本蒟蒻的第一篇水题题解,请大佬多多指出错误。
代码很长,请不要介意。
题目大意
给出 的字符地图(题目为 与 ),在地图中, 与 是终点与起点,
#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;
}