在C和H之间修建桥,先以H的行坐标优先,再纵坐标优先,找出H和C之间的最小距离此时的C和H 的横纵坐标。。直接遍历就可以~~
具体代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
const int maxn = 40;
const int minn = 0xffffff;
int main()
{
int row,col,i,j,k,w;
char str[maxn][maxn];
int a1,b1,a2,b2;
while(~scanf("%d %d",&row,&col)&&row&&col){
getchar();
int sum = minn;
for(i = 0; i < row;i++){
gets(str[i]);
}
for(i = 0 ;i < row; i++){
for(j = 0; j < col;j++){
if(str[i][j] == 'H'){
for(k = 0 ; k < row;k++){
for(w = 0;w < col;w++){
if(str[k][w] == 'C'){
if(abs(k-i) + abs(w-j) < sum){
sum = abs(k-i) + abs(w-j);
a1 = i;
b1 = j;
a2 = k;
b2 = w;
}
}
}
}
}
}
}
cout<<a1<<" "<<b1<<" "<<a2<<" "<<b2<<endl;
}
return 0;
}