ACM-

Posted 小小小的程序媛

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于ACM-的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数