zoukankan      html  css  js  c++  java
  • HDU 4478

    这道题纠结了很久,一开始想一层一层地算,但是各种空间超出,然后百度了人家的解法,还是不错的想法,就是说第n 步走到这里,第n+2步也会走到这里。

    值得注意的是它不会走当前所在的可能的位置。   看一个简单的图片,或许会一目了然.

    #include<iostream>
    #include<string>
    #include<string.h>
    #include<stdio.h>
    #include<queue>
    #include<math.h>
    using namespace std;
    int n,x,y,T;
    char mapp[110][110];
    int vis[110][110][2];
    struct node{
        int x;int y;
        int step;
    };
    queue<node> q;
    int dir[8][2]={{1,0},{1,1},{0,1},{-1,0},{-1,1},{-1,-1},{0,-1},{1,-1}};
    void bfs(){
        node temp,next;
        int f,h;
        while(!q.empty()){
            temp=q.front();
            q.pop();
            if(temp.step>=T) break;
            for(int i=0;i<8;i++){
                f=temp.x+dir[i][0];
                h=temp.y+dir[i][1];
                if(f>=1&&f<=n&&h>=1&&h<=n&&mapp[f][h]!='#'){
                    next.x=f;
                    next.y=h;
                    next.step=temp.step+1;
                    if(!vis[f][h][next.step%2]){
                    q.push(next);
                    vis[f][h][next.step%2]=1;
                    }
                }
            }
        }
    
        int yy=T%2,ans=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(vis[i][j][yy])
                {
                    ans++;
                }
            }
        }
    
        cout<<max(ans,1)<<endl;
    
    
    }
    int main(){
       int t;
       cin>>t;
       while(t--){
        cin>>n>>T>>x>>y;
        memset(mapp,0,sizeof(mapp));
        memset(vis,0,sizeof(vis));
        while(!q.empty()) q.pop();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++)
                cin>>mapp[i][j];
        }
        node tt;
        tt.x=x;tt.y=y;tt.step=0;
        vis[x][y][0]=1;
        q.push(tt);
    
       bfs();
    
       }
    
        return 0;
    }
  • 相关阅读:
    NOIP2011 D1T1 铺地毯
    NOIP2013 D1T3 货车运输 倍增LCA OR 并查集按秩合并
    POJ 2513 trie树+并查集判断无向图的欧拉路
    599. Minimum Index Sum of Two Lists
    594. Longest Harmonious Subsequence
    575. Distribute Candies
    554. Brick Wall
    535. Encode and Decode TinyURL(rand and srand)
    525. Contiguous Array
    500. Keyboard Row
  • 原文地址:https://www.cnblogs.com/wintersong/p/5237390.html
Copyright © 2011-2022 走看看