UVA 1533 (bfs+ State compression)

Source: Internet
Author: User

Test instructions: There is a chessboard as follows, black means a pawn, white means empty, the initial state of the board is a position n is empty, and other positions have pieces.


Each time you can select a piece in a straight line or a continuous number of pieces to jump to a blank position, and then this one or more pieces are taken away, ask at least a few steps to the board to take the pieces to the left one and the same position and the initial blank position. Outputs the starting and Lazi positions of several steps and each step. The solution of the output dictionary order is minimal, and the output impossible is not solved.

Puzzle: Because there are 15 positions on the board, you can use state compression to indicate the state of the Board, 1 for a pawn, 0 for a blank, and 6 for each pawn, so use a flag[i][j] array to indicate where I walk in the direction J, then BFS, change the checkerboard state with XOR or operation.

#include <stdio.h> #include <string.h> #include <queue>using namespace std;const int N = 15; int POS, mp[1 << n];int flag[15][6] = {{ -1,-1,-1,-1,2,3},{-1,1,-1,3,4,5},{1,-1,2,-1,5,6}, { -1,2,-1,5,7,8},{ 2,3,4,6,8,9},{3,-1,5,-1,9,10}, { -1,4,-1,8,11,12},{4,5,7,9,12,13},{5,6,8,10,13,14}, {6,-1,9,-1,14,15},{-1,7,-1,12 , -1,-1},{7,8,11,13,-1,-1}, {8,9,12,14,-1,-1},{9,10,13,15,-1,-1},{10,-1,14,-1,-1,-1}};struct P {int Step;intpath[N + 5][2];int s;int num;}; int BFS (int s) {queue<p> q;while (!q.empty ()) Q.pop ();  P A;A.S = s;a.num = 1;a.step = 0;q.push (a), while (!q.empty ()) {P u = Q.front (), Q.pop (), for (int i = 0; i <; i++) {if (U.S. & (1 << i)) {for (int j = 0; J < 6; J + +) {int cur = i;  P B = u;if (flag[i][j]! =-1 && b.s & (1 << (Flag[i][j]-1)) {while (cur! =-1 && b.s & (1 << cur) {b.s ^= (1 << cur); cur = flag[cur][j]-1;b.num++;} if (cur < 0) continue;b.s |= (1 << cur); b.step = U.step + 1;b.path[b.stEp][0] = i;b.path[b.step][1] = cur;b.num--;if (!mp[b.s]) {if (B.num = = && B.s & (1 << (pos-1))) {PR INTF ("%d\n%d%d", B.step, B.path[1][0] + 1, b.path[1][1] + 1); for (int k = 2; k <= b.step; k++) printf ("%d%d", b.path[ K][0] + 1, b.path[k][1] + 1);p rintf ("\ n"); return 1;} MP[B.S] = 1;q.push (b);}}}}} return 0;}  int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &pos), memset (MP, 0, sizeof (MP)); int temp = (1 << 1) ^ (1 << (pos-1)), mp[temp] = 1;int res = BFS (temp), if (!res) printf ("impossible\n");} return 0;}


UVA 1533 (bfs+ State compression)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.