A. DZY Loves Chessboard

原题链接

题目大意

n×m个由’.’ 和 ‘-’ 组成的格子,要求在’.'上放棋子,并且相邻的格子没有相同的棋子。

解题

先看下面的图片,我们可以得出一个格子相邻的地方没有相同的字符,可以得出结论,只要用一个字符来标记自己当前的字符,并将周围的都变成和自己不同的就可以了,那么这道题就可以做出来了。找到 ’ . ’ 的格子,并将四周的 ’ . ’ 变成不同的字符就好了。

在这里插入图片描述

代码
#include <bits/stdc++.h>

using namespace std;

#define For(i, k, n) for (int i = k; i < n; i ++)
#define IOS ios::sync_with_stdio(false)
typedef long long LL;
#define P acos(-1);
const int N = 1e5 + 10, M = 1e6 + 10;
int dx[4] = {0, 1, -1, 0}, dy[4] = {1, 0, 0, -1};
int n, m;

char q[111][111];

void dfs(int x, int y, bool f)
{
    if (f == 1)
        q[x][y] = 'B';
    else
        q[x][y] = 'W';

    For (i, 0, 4)
    {
        int xx = dx[i] + x, yy = dy[i] + y;

        if (xx >= 0 && xx < n && yy >= 0 && yy < m && q[xx][yy] == '.')
        {
            if (f == 1)
                dfs(xx, yy, 0);
            else
                dfs(xx, yy, 1);
        }
    }
}

int main()
{
    IOS;
    cin >> n >> m;

    For (i, 0, n)
    {
        For (j, 0, m)
            cin >> q[i][j];
    }

    For (i, 0, n)
    {
        For (j, 0, m)
        {
            if (q[i][j] == '.')
                dfs(i, j, 1);
        }
    }

    For (i, 0, n)
    {
        For (j, 0, m)
            cout << q[i][j];
        
        cout << '\n';
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Process: com.example.dzy, PID: 26008 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dzy/com.example.dzy.NavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.example.dzy.Fragment_1.<init>(Fragment_1.java:44) at com.example.dzy.NavigationActivity.initTab(NavigationActivity.java:39) at com.example.dzy.NavigationActivity.onCreate(NavigationActivity.java:27) at android.app.Activity.performCreate(Activity.java:7802) at android.app.Activity.performCreate(Activity.java:7791) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值