【題解】Green Judge g067: B.薑餅部落的危機

【題目敘述】http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=g067
【Tag】二維陣列、迴圈

#include <iostream>
using namespace std;
char grid[505][505];

int main() {
    int T, N, M;
    string s;
    cin >> T;
    while (T--){
        cin >> N >> M;
        for (int j = 0; j <= M+1; j++){
            grid[0][j] = '0'; //地圖外圍皆視同海面
        }
        for (int i = 1; i <= N; i++){
            cin >> s;
            grid[i][0] = '0'; //地圖外圍皆視同海面
            for (int j = 1; j <= M; j++){
                grid[i][j] = s[j-1];
            }
            grid[i][M+1] = '0'; //地圖外圍皆視同海面
        }
        for (int j = 0; j <= M+1; j++){
            grid[N+1][j] = '0'; //地圖外圍皆視同海面
        }
        int ans = 0;
        for (int i = 0; i <= N; i++){
            for (int j = 0; j <= M; j++){
                if (grid[i][j] != grid[i][j+1]){
                    ans++;
                }
                if (grid[i][j] != grid[i+1][j]){
                    ans++;
                }
            }
        }
        cout << ans << "\n";
    }
}
分享本文 Share with friends