예제 #1
0
파일: search.cs 프로젝트: zhangkun-bjtu/tmp
        public static int zong_num; //纵将的个数

        #endregion Fields

        #region Methods

        public static int bfs(int[,] matrix, string[] name, ref Queue<int[,]> ansinmatrix)
        {
            progress haha = new progress();
            haha.Show();
            zk[] data;
            try
            {
                data = new zk[45000000];
            }
            catch (System.OutOfMemoryException ex)
            {
                haha.Close();
                return -1;
            }
            int i, j;
            for (i = 0; i < 45000000; i++) data[i].vis = false;
            status init = new status();
            init.init();
            int[] universe = { 0, 1, 2, 3, 4, 5 };
            int flag = 0;
            matrix_to_status(universe, matrix, ref init);
            zk start;
            start.cnt = start.cnt_universe = start.pre = 0;
            start.vis = false;
            start.pre = -1; start.vis = true; status_to_num(init, ref start.cnt, ref start.cnt_universe);
            Queue<zk> key = new Queue<zk>();
            key.Enqueue(start);
            int end = start.cnt;
            while (key.Count != 0)
            {
                zk zk_temp = key.ElementAt(0);
                key.Dequeue();
                status cnt = new status();
                cnt.init();
                num_to_status(zk_temp.cnt, zk_temp.cnt_universe, ref cnt);
                for (i = 1; i <= 2; i++)
                {
                    for (j = 1; j <= 4; j++)
                    {
                        status temp = new status();
                        temp.init();
                        if (move(cnt, i, j, ref temp))
                        {
                            int temp_num = 0, temp_universe = 0;
                            status_to_num(temp, ref temp_num, ref temp_universe);
                            if (!data[temp_num].vis)
                            {
                                data[temp_num].vis = true;
                                data[temp_num].cnt = temp_num; data[temp_num].cnt_universe = temp_universe; data[temp_num].pre = zk_temp.cnt;
                                zk zk_temp1;
                                zk_temp1.cnt = temp_num; zk_temp1.cnt_universe = temp_universe; zk_temp1.pre = zk_temp.cnt;
                                zk_temp1.vis = true;
                                key.Enqueue(zk_temp1);
                                if (judge(temp)) { flag = zk_temp1.cnt; break; }
                            }
                        } if (flag != 0) break;
                    } if (flag != 0) break;
                } if (flag != 0) break;
            }
            haha.Close();
            if (flag == 0)
            {
                return 0;
            }
            Queue<int> yes = new Queue<int>();
            yes.Enqueue(flag);
            while (true)
            {
                flag = data[flag].pre;
                yes.Enqueue(flag);
                if (flag == end) break;
            }
            status getansmaxtrix = new status();
            getansmaxtrix.init();
            for (i = yes.Count() - 1; i >= 0; i--)
            {
                int[,] ansmatrix = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
                getansmaxtrix.init();
                num_to_status(data[yes.ElementAt(i)].cnt, data[yes.ElementAt(i)].cnt_universe, ref getansmaxtrix);
                status_to_matrix(getansmaxtrix, ansmatrix);
                for (int ii = 0; ii < 5; ii++)
                {
                    for (int jj = 0; jj < 4; jj++)
                    {
                        if (1 <= ansmatrix[ii, jj] && ansmatrix[ii, jj] <= 5)
                        {
                            ansmatrix[ii, jj] = getansmaxtrix.universe[ansmatrix[ii, jj]];
                        }
                    }
                }
                ansinmatrix.Enqueue(ansmatrix);
            }
            /*inputstatus haha = new inputstatus();
            haha.Show();
            for (i = 0; i <ansinmatrix.Count(); i++)
            {
                haha.show_num(i);
                haha.show_matrix(ansinmatrix.ElementAt(i));
            }*/
            ansform = new showans();
            ansform.Show();
            int[,] pre_matrix = new int[5, 4];
            int[,] cnt_matrix = new int[5, 4];
            status prestatus = new status();
            status cntstatus = new status();
            prestatus.init();
            cntstatus.init();
            name[6] = "曹操";
            for (int f**k = yes.Count() - 1; f**k >= 1; f**k--)
            {
                prestatus.init();
                cntstatus.init();
                num_to_status(data[yes.ElementAt(f**k)].cnt, data[yes.ElementAt(f**k)].cnt_universe, ref prestatus);
                num_to_status(data[yes.ElementAt(f**k - 1)].cnt, data[yes.ElementAt(f**k - 1)].cnt_universe, ref cntstatus);
                ansform.write_step(yes.Count() - f**k);
                status_to_matrix(prestatus, pre_matrix);
                for (i = 0; i < 5; i++)
                    for (j = 0; j < 4; j++)
                        if (1 <= pre_matrix[i, j] && pre_matrix[i, j] <= 5) pre_matrix[i, j] = prestatus.universe[pre_matrix[i, j]];
                status_to_matrix(cntstatus, cnt_matrix);
                for (i = 0; i < 5; i++)
                    for (j = 0; j < 4; j++)
                        if (1 <= cnt_matrix[i, j] && cnt_matrix[i, j] <= 5) cnt_matrix[i, j] = cntstatus.universe[cnt_matrix[i, j]];
                int[,] pre_pos = new int[11, 2];
                int[,] cnt_pos = new int[11, 2];
                for (i = 0; i < 11; i++) { for (j = 0; j < 2; j++) { pre_pos[i, j] = 0; } }
                for (i = 0; i < 11; i++) { for (j = 0; j < 2; j++) { cnt_pos[i, j] = 0; } }
                Queue<point> pre_bing = new Queue<point>();
                Queue<point> cnt_bing = new Queue<point>();
                point temp = new point();
                for (i = 0; i < 5; i++)
                {
                    for (j = 0; j < 4; j++)
                    {
                        if (pre_matrix[i, j] == 7) { temp.x = i + 1; temp.y = j + 1; pre_bing.Enqueue(temp); }
                        else if (pre_pos[pre_matrix[i, j], 0] == 0) { pre_pos[pre_matrix[i, j], 0] = i + 1; pre_pos[pre_matrix[i, j], 1] = j + 1; }
                        if (cnt_matrix[i, j] == 7) { temp.x = i + 1; temp.y = j + 1; cnt_bing.Enqueue(temp); }
                        else if (cnt_pos[cnt_matrix[i, j], 0] == 0) { cnt_pos[cnt_matrix[i, j], 0] = i + 1; cnt_pos[cnt_matrix[i, j], 1] = j + 1; }
                    }
                }
                int move_flag = 0;
                for (i = 1; i <= 6; i++)
                {
                    if (pre_pos[i, 0] - cnt_pos[i, 0] == 1) { ansform.write_word(name[i]); ansform.write_word_line("向上移动一格"); move_flag = 1; }
                    else if (pre_pos[i, 0] - cnt_pos[i, 0] == -1) { ansform.write_word(name[i]); ansform.write_word_line("向下移动一格"); move_flag = 1; }
                    if (pre_pos[i, 1] - cnt_pos[i, 1] == 1) { ansform.write_word(name[i]); ansform.write_word_line("向左移动一格"); move_flag = 1; }
                    if (pre_pos[i, 1] - cnt_pos[i, 1] == -1) { ansform.write_word(name[i]); ansform.write_word_line("向右移动一格"); move_flag = 1; }
                }
                if (move_flag == 1) continue;
                int[] pre_bing_vis = { 0, 0, 0, 0 };
                int[] cnt_bing_vis = { 0, 0, 0, 0 };
                for (i = 0; i < 4; i++)
                {
                    for (j = 0; j < 4; j++)
                    {
                        if (pre_bing.ElementAt(i).x == cnt_bing.ElementAt(j).x && pre_bing.ElementAt(i).y == cnt_bing.ElementAt(j).y) { pre_bing_vis[i] = cnt_bing_vis[j] = 1; }
                    }
                }
                int posi, posj;
                for (posi = 0; posi < 4; posi++) if (pre_bing_vis[posi] == 0) break;
                for (posj = 0; posj < 4; posj++) if (cnt_bing_vis[posj] == 0) break;
                if (pre_bing.ElementAt(posi).x - cnt_bing.ElementAt(posj).x == 1) { ansform.write_soilder(pre_bing.ElementAt(posi).x, pre_bing.ElementAt(posi).y, "向上移一位"); continue; }
                else if (pre_bing.ElementAt(posi).x - cnt_bing.ElementAt(posj).x == -1) { ansform.write_soilder(pre_bing.ElementAt(posi).x, pre_bing.ElementAt(posi).y, "向下移一位"); continue; }
                else if (pre_bing.ElementAt(posi).y - cnt_bing.ElementAt(posj).y == 1) { ansform.write_soilder(pre_bing.ElementAt(posi).x, pre_bing.ElementAt(posi).y, "向左移一位"); continue; }
                else if (pre_bing.ElementAt(posi).y - cnt_bing.ElementAt(posj).y == -1) { ansform.write_soilder(pre_bing.ElementAt(posi).x, pre_bing.ElementAt(posi).y, "向右移一位"); continue; }
                prestatus.init();
                cntstatus.init();
            }
            return 1;
        }