标签:size stdio.h ring ++ string main sum get space
首先用广度优先搜索来枚举出所有小人能够到达的点,并计算出每个点能够消灭的敌人的数量。如果下一个点能够消灭的敌人数量大于前一个,那么更新消灭敌人数量的最大值,并更新坐标点。下面是代码。
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
struct A{
int x,y;
};
int Next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
int book[20][20],n,m,Max,mx,my;
char Map[20][20];
void getnum(int i, int j)
{
int sum=0,x,y;
x = i, y = j;
while(Map[x][y] != ‘#‘){
if(Map[x][y] == ‘G‘)
sum++;
x--;
}
x = i, y = j;
while(Map[x][y] != ‘#‘){
if(Map[x][y] == ‘G‘)
sum++;
x++;
}
x = i, y = j;
while(Map[x][y] != ‘#‘){
if(Map[x][y] == ‘G‘)
sum++;
y--;
}
x = i, y = j;
while(Map[x][y] != ‘#‘){
if(Map[x][y] == ‘G‘)
sum++;
y++;
}
if(sum > Max){
Max = sum;
mx = i;
my = j;
}
}
void bfs(A head)
{
A h,t;
int i,tx,ty;
queue<A> q;
getnum(head.x,head.y);
q.push(head);
book[head.x][head.y] = 1;
while(!q.empty()){
h = q.front();
q.pop();
for(i = 0; i < 4; i++){
tx = h.x + Next[i][0];
ty = h.y + Next[i][1];
if(tx < 0 || tx >= n || ty < 0 || ty >= m)
continue;
if(Map[tx][ty] == ‘.‘ && book[tx][ty] == 0){
t.x = tx;
t.y = ty;
getnum(tx, ty);
q.push(t);
book[tx][ty] = 1;
}
if(tx == n-1 && ty == m-1)
return ;
}
}
}
int main()
{
A h;
int i,j;
while(~scanf("%d%d%d%d",&n,&m,&h.x,&h.y)){
memset(book,0,sizeof(book));
Max = 0;
mx = h.x;
my = h.y;
for(i = 0; i < n; i++)
scanf("%s",Map[i]);
bfs(h);
printf("(%d, %d) %d\n",mx,my,Max);
}
return 0;
}
/*输入样例:
13 13 3 3
#############
#GG.GGG#GGG.#
###.#G#G#G#G#
#.......#..G#
#G#.###.#G#G#
#GG.GGG.#.GG#
#G#.#G#.#.#.#
##G...G.....#
#G#.#G###.#G#
#...G#GGG.GG#
#G#.#G#G#.#G#
#GG.GGG#G.GG#
#############
输出:(7,11)10
*/
标签:size stdio.h ring ++ string main sum get space
原文地址:http://www.cnblogs.com/didideblog/p/6785795.html