廣搜模板——HDU ACM 1312解題報告

來源:互聯網
上載者:User

 戴哥哥講了很久的一道題,後續題的基礎

#include <iostream>
#include <queue>
using namespace std;
struct node
{
    int x,y;
};
int w,h,sx,sy,sum;
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
char map[25][25];
void BFS()
{
    queue<node> q;
    node n,p;
    n.x = sx;
    n.y = sy;
    q.push(n);
    map[sx][sy] = '#';
    while(!q.empty())
    {
        n = q.front();
        q.pop();
        //sum++;
        for(int i = 0;i < 4;i++)
        {
            int tx = n.x + dir[i][0];
            int ty = n.y + dir[i][1];
            //if(tx < 0 || tx >= w || ty < 0 || ty >= h)
                //continue;
            //if(map[tx][ty] == '#')
                //continue;
            if(tx >= 0 && tx < h && ty >= 0 && ty < w && map[tx][ty] == '.')
            {
                sum++;
                p.x = tx;
                p.y = ty;
                q.push(p);
                map[tx][ty] = '#';
            }
        }
    }
}
int main()
{
    int i,j;
    //freopen("1.txt","r",stdin);
    while(scanf("%d %d",&w,&h) == 2)
    {
        if(w == 0 && h == 0)
            break;

        for(i = 0;i < h;i++)
            for(j = 0;j < w;j++)
            {
                cin >> map[i][j];
                if(map[i][j] == '@')
                {
                    sx = i;
                    sy = j;
                }
            }
        sum = 1;
        BFS();
        printf("%d/n",sum);
    }
    return 0;
}

基本思想:從初始狀態S開始,利用規則,產生所有可能的狀態。構成樹的下一層節點,檢查是否出現目標狀態G,若未出現,就對該層所有狀態節點,分別順序利用規則。產生再下一層的所有狀態節點,對這一層的所有狀態節點檢查是否出現G,若未出現,繼續按上面思想產生再下一層的所有狀態節點,這樣一層一層往下展開。直到出現目標狀態為止。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.