Description
请你帮忙设计一个从城市M到城市Z的输油管道,现在已经把整个区域划分为R行C列,每个单元格可能是空的也可能是以下7种基本管道之一:
油从城市M流向Z,‘+’型管道比较特殊,因为石油必须在两个方向(垂直和水平)上传输,如下图所示:
现在恐怖分子弄到了输油管道的设计图,并把其中一个单元格中的管道偷走了,请你帮忙找到偷走的管道的位置以及形状。
Input
第一行包含两个整数R和C(1<=R,C<=25)。
接下来R行每行C个字符描述被偷之后的形状,字符分为以下三种:
(1)‘.’表示空;
(2)字符‘|’(ASCII为124)、‘-’、‘+’、‘1’、‘2’、‘3’、‘4’描述管道的形状;
(3)‘M’和‘Z’表示城市,两个都是只出现一次。
输入保证石油的流向是唯一的,只有一个管道跟M和Z相连,除此此外,保证没有多余的管道,也就是说所有的管道在加进被偷的管道后一定都会被用上。
输入保证有解而且是唯一的。
Output
输出被偷走的管道的行号和列号以及管道的类型。
Sample Input
输入1:
3 7
…
.M-.-Z.
…
输入2:
3 5
…1-M
1-+…
Z.23.
输入3:
6 10
Z.1----4…
|.|…|…
|…14…M…
2-+++4…
…2323…
…
Sample Output
输出1:
2 4 -
输出2:
2 4 4
输出3:
3 3 |
.
.
.
.
.
.
分析
暴力枚举判断
.
.
.
.
.
程序:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int r,c;
char a[30][30] ;
int main()
{
scanf("%d%d",&r,&c);
for (int i=1;i<=r;i++)
for (int j=1;j<=c;j++)
cin>>a[i][j];
for (int i=1;i<=r;i++)
{
bool bz=false;
for (int j=1;j<=c;j++)
if (a[i][j]=='.')
{
if (a[i-1][j]=='|'||a[i-1][j]=='+'||a[i-1][j]=='1'||a[i-1][j]=='4'||a[i-1][j]=='Z')
if (a[i][j-1]=='|'||a[i][j-1]=='3'||a[i][j-1]=='4'||a[i][j-1]=='.'||j-1==0)
if (a[i][j+1]=='|'||a[i][j+1]=='1'||a[i][j+1]=='2'||a[i][j+1]=='.'||j+1==c+1)
if (a[i+1][j]=='|'||a[i+1][j]=='+'||a[i+1][j]=='2'||a[i+1][j]=='3'||a[i+1][j]=='Z')
{
printf("%d %d %c",i,j,'|');
break;
}
if (a[i-1][j]=='-'||a[i-1][j]=='.'||a[i-1][j]=='1'||a[i-1][j]=='4'||i-1==0)
if (a[i][j-1]=='-'||a[i][j-1]=='+'||a[i][j-1]=='1'||a[i][j-1]=='2'||a[i][j-1]=='Z')
if (a[i][j+1]=='-'||a[i][j+1]=='+'||a[i][j+1]=='3'||a[i][j+1]=='4'||a[i][j+1]=='Z')
if (a[i+1][j]=='-'||a[i+1][j]=='1'||a[i+1][j]=='4'||a[i+1][j]=='.'||i+1==r+1)
{
printf("%d %d %c",i,j,'-');
break;
}
if (a[i-1][j]=='+'||a[i-1][j]=='|'||a[i-1][j]=='1'||a[i-1][j]=='4'||a[i-1][j]=='Z')
if (a[i][j-1]=='+'||a[i][j-1]=='-'||a[i][j-1]=='1'||a[i][j-1]=='2'||a[i][j-1]=='Z')
if (a[i][j+1]=='+'||a[i][j+1]=='-'||a[i][j+1]=='3'||a[i][j+1]=='4'||a[i][j+1]=='Z')
if (a[i+1][j]=='+'||a[i+1][j]=='|'||a[i+1][j]=='2'||a[i+1][j]=='3'||a[i+1][j]=='Z')
{
printf("%d %d %c",i,j,'+');
break;
}
if (a[i-1][j]=='-'||a[i-1][j]=='2'||a[i-1][j]=='3'||a[i-1][j]=='.'||i-1==0)
if (a[i][j-1]=='|'||a[i][j-1]=='3'||a[i][j-1]=='4'||a[i][j-1]=='.'||j-1==0)
if (a[i][j+1]=='-'||a[i][j+1]=='+'||a[i][j+1]=='3'||a[i][j+1]=='4'||a[i][j+1]=='Z')
if (a[i+1][j]=='|'||a[i+1][j]=='+'||a[i+1][j]=='2'||a[i+1][j]=='3'||a[i+1][j]=='Z')
{
printf("%d %d %c",i,j,'1');
break;
}
if (a[i-1][j]=='|'||a[i-1][j]=='+'||a[i-1][j]=='1'||a[i-1][j]=='4'||a[i-1][j]=='Z')
if (a[i][j-1]=='|'||a[i][j-1]=='3'||a[i][j-1]=='4'||a[i][j-1]=='.'||j-1==0)
if (a[i][j+1]=='-'||a[i][j+1]=='+'||a[i][j+1]=='3'||a[i][j+1]=='4'||a[i][j+1]=='Z')
if (a[i+1][j]=='-'||a[i+1][j]=='1'||a[i+1][j]=='4'||a[i+1][j]=='.'||i+1==r+1)
{
printf("%d %d %c",i,j,'2');
break;
}
if (a[i-1][j]=='|'||a[i-1][j]=='+'||a[i-1][j]=='1'||a[i-1][j]=='4'||a[i-1][j]=='Z')
if (a[i][j-1]=='-'||a[i][j-1]=='+'||a[i][j-1]=='1'||a[i][j-1]=='2'||a[i][j-1]=='Z')
if (a[i][j+1]=='|'||a[i][j+1]=='1'||a[i][j+1]=='2'||a[i][j+1]=='.'||j+1==c+1)
if (a[i+1][j]=='-'||a[i+1][j]=='1'||a[i+1][j]=='4'||a[i+1][j]=='.'||i+1==r+1)
{
printf("%d %d %c",i,j,'3');
break;
}
if (a[i-1][j]=='-'||a[i-1][j]=='.'||a[i-1][j]=='2'||a[i-1][j]=='3'||i-1==0)
if (a[i][j-1]=='-'||a[i][j-1]=='+'||a[i][j-1]=='1'||a[i][j-1]=='2'||a[i][j-1]=='Z')
if (a[i][j+1]=='|'||a[i][j+1]=='1'||a[i][j+1]=='2'||a[i][j+1]=='.'||j+1==c+1)
if (a[i+1][j]=='|'||a[i+1][j]=='+'||a[i+1][j]=='2'||a[i+1][j]=='3'||a[i+1][j]=='Z')
{
printf("%d %d %c",i,j,'4');
break;
}
}
if (bz==true) break;
}
return 0;
}