为防止广告,目前nocow只有登录用户能够创建新页面。如要创建页面请先登录/注册(新用户需要等待1个小时才能正常使用该功能)。

Sgu/125

来自NOCOW
< Sgu
跳转到: 导航, 搜索

暴力搜,当前格子搜完去判断上方的格子。

#include <stdio.h>
#include <stdlib.h>
using namespace std;
 
int n, A[3][3], B[3][3];
 
bool check(int x, int y)
{
     int cnt = 0;
     if (x && A[x - 1][y] > A[x][y]) cnt++;
     if (x < n - 1 && A[x + 1][y] > A[x][y]) cnt++;
     if (y && A[x][y - 1] > A[x][y]) cnt++;
     if (y < n - 1 && A[x][y + 1] > A[x][y]) cnt++;
     return (cnt == B[x][y]); 
}
 
void dfs(int x, int y)
{
     if (y == n) { x++; y = 0; }
     if (x == n)
     {
        for (int i = 0; i < n; ++i)
            if (!check(n-1, i)) return;
        for (int i = 0; i < n; ++i)
        {
            printf("%d", A[i][0]);
            for (int j = 1; j < n; ++j)
                printf(" %d", A[i][j]);
            printf("\n");
        }
        exit(0);
     }
     for (int i = 0; i < 10; ++i)
     {
         A[x][y] = i;
         if (!x || check(x - 1, y)) dfs(x, y+1);
     }
}
 
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            scanf("%d", &B[i][j]);
    dfs(0, 0);
    printf("NO SOLUTION\n");
    return 0;
}
// From FingerSed
个人工具