/*
 *author:junyi sun @ccnu
* e-mail:[email protected]
*/
using system;
namespace sunjoy
{
    public class queen
    {
        public static int main()
        {
            int board_size = 0,x=0,y=0;//棋盤大小,當前行,當前列
            uint solution_count = 0;   //皇后擺放方案的個數
            int[] rows, cols, slot1, slot2, x2y;//行占用情況,列占用情況,“/”狀斜線占用情況,“/”狀斜線占用情況,皇后坐標
            datetime t_start, t_end;
            
            console.writeline("請輸入棋盤的大小");
            try
            {
                board_size = convert.toint32(console.readline());
                if (board_size <= 0)
                {
                    console.writeline("非法數據");
                    return -1;
                }
            }
            catch (exception e) { console.writeline("發生異常" + e.message); };
            rows = new int[board_size];
            cols = new int[board_size];
            slot1 = new int[board_size * 2 - 1];
            slot2 = new int[board_size * 2 - 1];
            x2y = new int[board_size];
            for (int i = 0; i < board_size; i++)
                x2y[i] = -1;   //坐標初始化都為-1
            t_start = datetime.now;
            while (true)
            {
                for (y = x2y[x]+1; y < board_size; y++)
                    if (rows[x] == 0 && cols[y] == 0 && slot1[x + y] == 0 && slot2[x - y + board_size - 1] == 0) 
                        break;
                if (y < board_size)
                {
                    //第x行的棋子落下
                    rows[x] = 1; cols[y] = 1; slot1[x + y] = 1; slot2[x - y + board_size - 1] = 1; x2y[x] = y;
                }   
                else
                {
                    //回溯,拿起棋子
                    if (x > 0)
                    {
                        x2y[x] = -1;
                        x--;
                        rows[x] = 0; cols[x2y[x]] = 0; slot1[x + x2y[x]] = 0; slot2[x - x2y[x] + board_size - 1] = 0;
                        continue;
                    }
                    else break;
                }
                if (x == board_size - 1)   //如果已經得到了一組解,即當最后一行棋子落定之時
                {
                    for (int i = 0; i < board_size; i++)
                    {
                        for (int j = 0; j < board_size; j++)
                        {
                            if (x2y[i] == j) console.write("q");
                            else console.write("■");
                        }
                        console.write("/n");
                        
                    }
                    console.write("/n");
                    solution_count++;   //總方案數加一
                    rows[x] = 0; cols[x2y[x]] = 0; slot1[x + x2y[x]] = 0; slot2[x - x2y[x] + board_size - 1] = 0;//放棄這一列
                }
                else
                {
                    x++;   //繼續處理下一行
                }
            }
            t_end = datetime.now;
            console.writeline("總共{0}組解",solution_count);
            console.writeline("計算及打印共用時間{0}秒",t_end-t_start);
            return 0;
        }
    }
}
___________________________________________________________
測試結果:
請輸入棋盤的大小
4
■q■■
■■■q
q■■■
■■q■
■■q■
q■■■
■■■q
■q■■
總共2組解
計算及打印共用時間00:00:00秒
_______________________________________________
請輸入棋盤的大小
6
■q■■■■
■■■q■■
■■■■■q
q■■■■■
■■q■■■
■■■■q■
■■q■■■
■■■■■q
■q■■■■
■■■■q■
q■■■■■
■■■q■■
■■■q■■
q■■■■■
■■■■q■
■q■■■■
■■■■■q
■■q■■■
■■■■q■
■■q■■■
q■■■■■
■■■■■q
■■■q■■
■q■■■■
總共4組解
計算及打印共用時間00:00:00.0156250秒
新聞熱點
疑難解答