您的位置:首页 > 运维架构

【搜索水题】hdu hdoj How Many People Can Survive

2010-04-28 06:41 513 查看
郁闷,,这么水的题目。。。

那天比赛真的是nc了。。。。看到搜索题就怕。。。

另外。。。

BFS竟然有点忘记了。。。罪过罪过。。。。。



Netbeans的调试还是很不习惯。。。太可怕了。。。



纯BFS搜索题目。。。。代码写的非常搓。。。。用了3个GOTO。。。。



How Many People Can Survive 
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia 
Font Size: ← →
Problem Description
Two opposing armies are lost in a big forest. The forest is dangerous ,because it is filled with poisonous gas . People can only survive by staying at the place that is surrounded by trees.As the two armies are opposing, if one army are able to move to the other one , they wil fight with each other. Only the army that with the more people will survive, if the number is equal, no one will survive. Now you are asked to find out how many people will survive. 
Input
There are several test cases.
Each case begins with an integer n and m (3<=n,m<=300) stands for the size of forest.
Then follows n lines , each line contains m characters .
‘.’ Stands for empty place 
‘#’Stands for tree and can’t been walked through
‘o’Stands for the first army
‘v’stands for the second army 
Output
The output contains one line for each data set : two integers, p and q, stands for the number of the first army that can survive and the number of the second army that can survive. 
Sample Input
3 3
..#
ovo
…
10 10
...vvvoo..
##########
#....oo.o#
#..v.v...#
#........#
#..####..#
#..#o.#..#
#..#.v#..#
#..####..#
##########
Sample Output
0 0
3 0
Author
dandelion 
Source
HDU 8th Programming Contest Online




/* 
 * File:   main.cpp
 * Author: Administrator
 *
 * Created on 2010年4月28日, 上午5:03
 */

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<deque>
using namespace std;

/*
 * 
 */
struct PT {
    int x;
    int y;
};

void Fill(int n, int m, char Graph[][305]) {
    int i, j;
    for (i = 0; i <= n + 1; i++)
        Graph[i][0] = Graph[i][m + 1] = '*';
    for (i = 0; i <= m + 1; i++)
        Graph[0][i] = Graph[n + 1][i] = '*';
}

void BFS(int n, int m, char Graph[][305], int &O, int &V) {
    int i, j;
    int numO, numV, sumO = 0, sumV = 0;
    int plusX[] = {0, 1, 0, -1};
    int plusY[] = {1, 0, -1, 0};
    bool IsSurvive;
    deque<PT> Queue;
    int top = 0;
    PT pt, get;
    bool IsReturn;

RETURN:
    IsReturn = false;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
            if (Graph[i][j] != '#' && Graph[i][j] != '-') {
                pt.x = i;
                pt.y = j;
                Queue.push_back(pt);
                IsReturn = true;
                goto BFSSTART;
            }
BFSSTART:
    IsSurvive = true;
    numO = 0;
    numV = 0;
    while (!Queue.empty()) {
        get = Queue.front();
        Queue.pop_front();
		if (Graph[get.x][get.y] == 'v')
			numV++;
		if (Graph[get.x][get.y] == 'o')
			numO++;
		Graph[get.x][get.y] = '-';

        for (i = 0; i < 4; i++) {
            if (Graph[get.x + plusX[i]][get.y + plusY[i]] == '*')
                IsSurvive = false;
            if (Graph[get.x + plusX[i]][get.y + plusY[i]] == 'v') {
                pt.x = get.x + plusX[i];
                pt.y = get.y + plusY[i];
                Queue.push_back(pt);
				numV++;
				Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
            }
            if (Graph[get.x + plusX[i]][get.y + plusY[i]] == 'o') {
                pt.x = get.x + plusX[i];
                pt.y = get.y + plusY[i];
                Queue.push_back(pt);
				numO++;
				Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
            }
            if (Graph[get.x + plusX[i]][get.y + plusY[i]] == '.') {
                pt.x = get.x + plusX[i];
                pt.y = get.y + plusY[i];
                Queue.push_back(pt);
				Graph[get.x + plusX[i]][get.y + plusY[i]] = '-';
            }
        }
    }
    if (IsSurvive) {
        if (numV > numO)
            sumV += numV;
        else if (numV < numO)
            sumO += numO;
    }
    if (IsReturn)
        goto RETURN;
ANS:
    O = sumO;
    V = sumV;
}

int main() {
    char Graph[305][305];
    int n, m;
    int i, j;
    int O, V;
    while (scanf("%d%d", &n, &m) != EOF) {
        for (i = 1; i <= n; i++)
            scanf("%s", &Graph[i][1]);
        Fill(n, m, Graph);
        /*
        for (i = 0; i <= n + 1; i++) {
            for (j = 0; j <= m + 1; j++)
                printf("%c", Graph[i][j]);
            printf("/n");
        }
         */
        BFS(n, m, Graph, O, V);
        printf("%d %d/n", O, V);
    }
    return (EXIT_SUCCESS);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: