POJ 1753 bfs+ bit operation

Source: Internet
Author: User

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

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.