The t_t + + operator is not the same as +1. (i+1)%4 with parentheses. The BFS function is a bool type, forgetting to return false when the case. Oh.... Debug is going to cry ...
DESCRIPTION:
The least steps. Make all the pieces on the board black or white. Odd number of times is equivalent to 1 times. An even number of times is equal to not turning.
BFS is used to solve the optimal problem. It is mainly used to find the shortest path from the initial state path. Thought is the brute force enumeration + bitwise operation. The first time to do the problem of bit arithmetic. Drunk and drunken ...
Attached code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#define END1 65535
#define END2 0
using namespace Std;
int STEP[END1];
BOOL Used[end1];
unsigned short q[end1];
int front = 0, rear = 0;
void Init ()
{
Char str;
Front = 0, rear = 0;
unsigned short int begin = 0;
for (int i=0; i<4; ++i)
{
for (int j=0; j<4; ++j)
{
CIN >> str;
if (str = = ' B ')
Begin |= (1 << (i*4 + j));
}
}
memset (step, 0, sizeof (step));
memset (used, 0, sizeof (used));
q[rear++] = begin;
Used[begin] = 1;
Step[begin] = 0;
}
unsigned short int move (unsigned short int state, int i)
{
unsigned short int temp = 0;
Temp |= (1 << i);
if ((i + 1)% 4! = 0)
Temp |= (1 << (i + 1));
if (i% 4! = 0)
Temp |= (1 << (i-1));
if (i + 4 < 16)
Temp |= (1 << (i + 4));
if (i-4 >= 0)
Temp |= (1 << (i-4));
return (STATE^TEMP);
}
BOOL BFs ()
{
while (Front < rear)
{
unsigned short state = q[front++];
for (int i=0; i<16; ++i)
{
unsigned short temp;
temp = Move (state, I);
if (0 = = State | | 65535 = = state)
{
cout << Step[state];
return true;
}
else if (!used[temp])
{
q[rear++] = temp;
Used[temp] = true;
Step[temp] = step[state]+1;
}
}
}
return false;
}
int main ()
{
Init ();
if (!BFS ())
cout << "Impossible";
char c;
CIN >> C;
return 0;
}
POJ 1753 bfs+ bit operation