ACM-

解题思路:Find Gold

Someone locked you in the grid house. You can’t escape, until you find the enough gold. But somewhere is on the fire, so you can’t stand on this place. You can go with eight direction: up down, left, right, left-up, left-down, right-up, right-down. Sure, your first position is not fired.

输入

The input will contain one or more grid. Each grid is preceded by a line containing the number of rows and columns in the grid and the row and column of the start position. All numbers are in the range 1-25. The rows of the grid follow, starting on the next line, consisting of 'O','.' and 'X' characters. 'O' stand for gold, '.' stand for fire, 'X' stand for there are nothing . The end of the input is indicated by a line containing four zeros. The numbers on any one line are separated by blanks. The grid rows contain no blanks.

输出

For each grid in the input, the output contains a single line with the max number of the gold you can get.

样例输入

4 6 2 2
. . . X X X 
. X . O . O 
X . . . . . 
X X X . . . 
6 6 4 5
X X O . X O 
O O X . O O 
. . . . . . 
X X X . O O 
X . X . . . 
O X X . . O 
4 7 1 2
. X . . . . X 
. X X . X X . 
. . X X O O X 
. O X X . X O
0 0 0 0

样例输出

0
2
4

深搜的水题。。。。。唯一麻烦的字符串的读入读出,为了避免麻烦,最好还是用cincout比较好。

// Find Gold.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
const int M = 10005;
int n, m, sr, sl, ans, vis[M][M], dir[8][2] = { 1, 0, -1, 0, 0, 1, 0, -1, 1, 1, -1, -1, 1, -1, -1, 1 };
char map[M][M];

void DFS(int x, int y)
{
    vis[x][y] = 1;
    for (int i = 0; i < 8; i++)
    {
        int nx = dir[i][0] + x;
        int ny = dir[i][1] + y;
        if (nx>=0 && ny>=0 && nx<n && ny<m && !vis[nx][ny] && map[nx][ny] != '.')
        {
            if (map[nx][ny] == 'O')
                ans++;
            DFS(nx, ny);
        }
    }
}

int main()
{
    while (scanf("%d %d %d %d", &n, &m, &sr, &sl))
    {
        if (n == 0 && m == 0 && sr == 0 && sl == 0) break;
        memset(vis, 0, sizeof(vis));
        memset(map, '\0', sizeof(map));
        ans = 0;
        for (int i = 0; i < n; i++)
        {
            getchar();//读取回车
            for (int j = 0; j < m; j++)
            {
                char c = getchar();
                map[i][j] = c;
                getchar();//读取空格
            }
        }
        if (map[sr - 1][sl - 1] == 'O') ans++;
        DFS(sr - 1, sl - 1);
        printf("%d\n", ans);

    }
    return 0;
}

 

posted @ 2018-03-04 18:25  小小小的程序猿  阅读(156)  评论(0编辑  收藏  举报
window.onload = function(){ $("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") }