• 回溯____蓝桥 棋盘


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int fenzu[6][6];
    int used_fenzu[6][6];//根据分组信息确定第一位;根据字母排列A B C D E F确定第二位;最终依靠本数组实现"组内判重"
    int used_row[6][6];//第一位为行数
    int used_col[6][6];//第一位为列数
    char map[6][6];
    int next_row[40];
    int next_col[40];
    int all = 1;
    
    void print()
    {
        int i, j;
        for(i = 0; i < 6; i++){for(j = 0; j < 6; j++){printf("%c",map[i][j]);}printf("
    ");}
    }
    
    int cango(int r, int c, char ch)
    {
        if(used_fenzu[ fenzu[r][c] ][ ch-'A' ] == 1 || used_row[r][ch-'A'] == 1 || used_col[c][ch-'A'] == 1)
            return 0;
        return 1;    
    }
    
    void fun(int r, int c, int n)
    {
        char ch;
        int i, j;
        
        //printf("r = %d, c = %d, n = %d
    ",r,c,n);
        if(n == 36)
        {
            printf("%d
    ",all++);
            print();    
            return;
        }
    
        for(ch = 'A'; ch <= 'F'; ch++)
        {
            if( cango(r, c, ch) )
            {
                map[r][c] = ch;
                used_fenzu[ fenzu[r][c] ][ ch-'A' ] = 1;
                used_row[r][ch-'A'] = 1;
                used_col[c][ch-'A'] = 1;
    
                fun(next_row[n+1], next_col[n+1], n+1);            
    
                map[r][c] = 0;
                used_fenzu[ fenzu[r][c] ][ ch-'A' ] = 0;
                used_row[r][ch-'A'] = 0;
                used_col[c][ch-'A'] = 0;
            }
        }
    
        return;
    }
    
    int main()
    {
        int N, i, j, temp_N, index;
        freopen("1.txt", "r", stdin);
        memset(map, 0, sizeof(map));
    
        for(i = 0; i < 6; i++)
        {
            for(j = 0; j < 6; j++)
            {
                scanf("%1d", &fenzu[i][j]);
            }
        }
        scanf("%d", &N);
    
        temp_N = N;
        while(temp_N--)
        {
            int r, c;
            char ch;
    
            scanf("%1d%1d%c", &r, &c, &ch);        
            map[r][c] = ch;
            used_fenzu[ fenzu[r][c] ][ ch-'A' ] = 1;
            used_row[r][ch-'A'] = 1;
            used_col[c][ch-'A'] = 1;
        }
    
        index = N;
        for(i = 0; i < 6; i++)
        {
            for(j = 0; j < 6; j++)
            {
                if( map[i][j]  == 0)
                {
                    next_row[index] = i;
                    next_col[index] = j;
                    index++;
                }
            }
        }    
        fun(next_row[N],next_col[N],N);   
        
        return 0;
    }
  • 相关阅读:
    Active Report 3 生成图表
    js asp.net enable/disable validator
    [.net] 如何在mail的加入正文显示图片
    [javascript] 得到 javascript 的当前文件名
    Colorful Stones
    Jam的计数法

    Beautiful Matrix
    能量项链
    Maxim and Discounts
  • 原文地址:https://www.cnblogs.com/wwjyt/p/3172723.html
Copyright © 2020-2023  润新知