原题
分析
题目简单,但是其中下标注意不要写错,用devcpp不习惯,整体替换时多替换了,导致后面出现问题,花了一会儿,送分题不能浪费太多时间。
#include <stdio.h>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
char c[110][110];
typedef struct point{
int x,y;
}point;
point s,e;
char cr[110][110]={
{"*...*....."},
{"......*..."},
{"...*...*.."},
{".........."},
{"...*.C...."},
{"*.....*..."},
{"...*......"},
{"..M......*"},
{"...*.*...."},
{".*.*......"}
};
int main()
{
for (int i=1;i<=10;i++){
for (int j=1;j<=10;j++){
c[i][j]=cr[i-1][j-1];
if (c[i][j]=='C')s.x=i,s.y=j;
else if (c[i][j]=='M')e.x=i,e.y=j;
}
}
int m1=0,m2=0;
int x1=s.x,y1=s.y,x2=e.x,y2=e.y;
c[x1][y1]='.';
c[x2][y2]='.';
int sum=0;
while (1){
if (m1==0){
x1--;
if (c[x1][y1]=='*'||x1<=0 ){
m1=1;
x1++;
}
}else if (m1==1){
y1++;
if (c[x1][y1]=='*'||y1>10){
m1=2;
y1--;
}
}else if (m1==2){
x1++;
if (c[x1][y1]=='*'||x1>10){
m1=3;
x1--;
}
}else{
y1--;
if (c[x1][y1]=='*'||y1<=0){
m1=0;
y1++;
}
}
if (m2==0){
x2--;
if (c[x2][y2]=='*'||x2<=0){
m2=1;
x2++;
}
}else if (m2==1){
y2++;
if (c[x2][y2]=='*'||y2>10){
m2=2;
y2--;
}
}else if (m2==2){
x2++;
if (c[x2][y2]=='*'||x2>10){
m2=3;
x2--;
}
}else{
y2--;
if (c[x2][y2]=='*'||y2<=0){
m2=0;
y2++;
}
}
sum++;
if (x1==x2 && y1==y2){
break;
}
}
cout<<sum<<endl;
return 0;
}
|