zoukankan      html  css  js  c++  java
  • HDU1312 Red and Black 解读

    递归搜索方法标题,采用递归搜索方法,但是,如果没有迭代计算的真正的政党格。

    我们的想法是:

    1 每一个搜索为党格要改变电流方向格的值至 ‘*’,或任何其他非'.'的值,代表方格了

    2 递归的时候不回复这个方格的值,就实际上不用反复搜索这个方格了。故此不用回溯


    #include <stdio.h>
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    int R, C, blacks;
    vector<string> board;
    
    inline bool isLegal(int r, int c)
    {
        return r>=0 && c>=0 && r<R && c<C && board[r][c] == '.';
    }
    
    void getBlacks(int r, int c)
    {
        blacks++;
        board[r][c] = '*';
        if (isLegal(r-1, c)) getBlacks(r-1, c);
        if (isLegal(r+1, c)) getBlacks(r+1, c);
        if (isLegal(r, c-1)) getBlacks(r, c-1);
        if (isLegal(r, c+1)) getBlacks(r, c+1);
    }
    
    int main()
    {
        string s;
        while (cin>>C>>R && C)
        {
            board.clear();
            for (int i = 0; i < R; i++)
            {
                cin>>s;
                board.push_back(s);
            }
            blacks = 0;
            for (unsigned i = 0; i < board.size(); i++)
            {
                for (unsigned j = 0; j < board[0].size(); j++)
                {
                    if ('@' == board[i][j])
                    {
                        getBlacks((int)i, (int)j);
                        goto out;
                    }
                }
            }
    out:;
            printf("%d
    ", blacks);
        }
        return 0;
    }



    版权声明:笔者心脏靖。景空间地址:http://blog.csdn.net/kenden23/,可能不会在未经作者同意转载。

  • 相关阅读:
    异步与回调的设计哲学
    CTF之PHP黑魔法总结
    图片隐写分离
    phpMyadmin各版本漏洞
    python 多线程
    order by name 注入
    Python lambda
    Python os.popen() 方法
    你和大牛差了啥
    Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more mirrors to try.
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4676320.html
Copyright © 2011-2022 走看看