山东省第七届ACM大学生程序设计竞赛 E题 The Binding of Isaac 简单搜索

题意:

具体翻译不了,大致:给一个n*m的矩阵,分别是点和#; 点表示空房子,#表示公共房子,找钥匙,钥匙可能在房子的边缘部分,也可能在空房子里,但是空房子必须附近只有一个公共房子。

分析:

上来一看水题,,,查找了所有#的边缘就交了 wa了,,,,,,,然后问了队友,翻译了下,还有另外一种情况,空房子里,就重新添加另一种判定搜索点的附近公共房子为1的,ac

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <algorithm>
#include <queue>
#include <map>
#define ll long long;
using namespace std;
const int maxn = 105;
const int mod = 1e9+7;
char ch[maxn][maxn];
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
int cnt = 0;
void dfs(int x,int y,int n,int m){
    for(int i=  0; i<4;i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx < 0 || tx >= n || ty < 0 ||ty >= m){
            cnt++;
        }
    }
}
int dfs2(int x,int y,int n,int m){
    int cnt1 = 0;
    for(int i=  0; i<4;i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx >= 0 && tx < n && ty >= 0 && ty < m){
            if(ch[tx][ty] == '#'){
                cnt1++;
            }
        }
    }
    return cnt1;
}
int main(){
    std::ios::sync_with_stdio(false);
    int T;cin>>T;
    while(T--){
        cnt = 0;
        int n,m;
        cin>>n>>m;
        for(int i = 0; i < n;i++){
            for(int j = 0; j<m;j++){
                cin>>ch[i][j];
            }
        }
        for(int i = 0;i < n;i++){
            for(int j = 0; j<m;j++){
                if(ch[i][j]=='#'){
                    dfs(i,j,n,m);
                }
                if(ch[i][j] == '.'){
                    if(dfs2(i,j,n,m) == 1){
                        cnt++;
                    }
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shensiback/article/details/80167165