题解:P13371 [GCJ 2011 #1C] Square Tiles

· · 题解

Solution

枚举 i,j,若当前为 # 字符,则将其视为左上角,并遍历四个位置,不满足要求输出 Impossible,否则将其替换。

Code

#include<bits/stdc++.h>
using namespace std;
const int N = 55;
int t, n, m;
int dx[5] = {0, 0, 0, 1, 1}, dy[5] = {0, 0, 1, 0, 1}, nx, ny;
char s[N][N], ans[5] = {'*', '/', '\\', '\\', '/'};
inline void solve(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> s[i][j];
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(s[i][j] != '#') continue;
            for(int k = 1; k <= 4; k++){
                nx = i + dx[k];
                ny = j + dy[k];
                if(nx > n || ny > m || s[nx][ny] != '#'){
                    cout << "Impossible\n";
                    return ;
                }
                s[nx][ny] = ans[k];
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++) cout << s[i][j];
        cout << '\n';
    }
    return ;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> t;
    for(int kk = 1; kk <= t; kk++){
        cout << "Case #" << kk << ":\n";
        solve();
    }
    return 0;
}