zoukankan      html  css  js  c++  java
  • fzu 1920 Left Mouse Button(简单深搜题)

    题目地址:http://acm.fzu.edu.cn/problem.php?pid=1920

    题目大意是给定一个n*n的图,模拟扫雷游戏,0代表没有雷区,1代表附近九宫格内只有一个雷……

    如果不懂的话去玩下扫雷,挺好玩的,99个雷的玩了好几百盘才赢了一次。。。。。。。。。。。。

    此题假设神操作,点不到雷,问你最少要多少下才可以把图中非雷的点完,怎么样才最快呢?

    当然先点0啦,,,,,,,,

    好吧,不废话了。。。。。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int dir[8][2]={{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0}};
    char map[10][10];
    int n;
    
    void dfs(int x,int y)
    {
        int xx;
        int yy;
        map[x][y]='#';
        for(int i=0;i<8;i++)
        {
            xx=x+dir[i][0];
            yy=y+dir[i][1];
            if(xx<0||xx>=n||yy<0||yy>=n)
            {
                continue;
            }
            //printf("%d%d^^",xx,yy);
            if(map[xx][yy]=='0')
            {
                dfs(xx,yy);
            }
            if(map[xx][yy]!='#'&&map[xx][yy]!='@')
            {
                map[xx][yy]='#';
            }
    
        }
    }
    
    int main()
    {
        int i,j,k,t,s;
        scanf("%d",&t);
        for(s=0;s<t;s++)
        {
            int count=0;
            scanf("%d",&n);
            getchar();
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    scanf("%c",&map[i][j]);
                }
                getchar();
            }
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(map[i][j]=='0')
                    {
                        dfs(i,j);
                        count++;
                    }
                }
            }
    
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(map[i][j]!='#'&&map[i][j]!='@')
                    {
                        count++;
                    }
                }
            }
            printf("Case %d: %d
    ",s+1,count);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/DFS.html
Copyright © 2011-2022 走看看