HDU 1429 bfs+ State compression

Source: Internet
Author: User

Open a three-dimensional marker array to mark whether Ignatius has reached the position with the key.
Since the number of keys is up to 10, you can use state compression to do
Use 1 and 0 to indicate that there is no key I, so that this person can get the key state to be used in binary number representation
Find the minimum value with BFS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace Std;
const int MAXN = 30;
const int inf = 0X7FFFFFFF;
int vis[maxn][maxn][1<<10];
Char MAP[MAXN][MAXN];
int ans = ans;
struct node
{
int x, y;
int step;
int state;
};
int st_x, st_y;
int n, m;
int dx[4] = { -1,0,1,0};
int dy[4] = {0,1,0,-1};
Queue<struct node> que;
void BFs ()
{
while (Que.size ())
Que.pop ();
memset (Vis, 0, sizeof (VIS));
struct node first = {st_x, st_y, 0, 0};
Vis[st_x][st_y][0] = 1;
Que.push (first);
while (Que.size ())
{
struct node now = Que.front ();
Que.pop ();
if (map[now.x][now.y] = = ' ^ ')
{
ans = now.step;
Break
}
for (int i = 0;i < 4;i++)
{
struct node next;
Next.x = now.x + dx[i];
Next.y = Now.y + dy[i];
if (map[next.x][next.y] = = ' * ' | | next.x < 1 | | next.x > N | | Next.y < 1 | | Next.y > M)
Continue;
if (Map[next.x][next.y] >= ' A ' && map[next.x][next.y] <= ' J ')
if (! ( Now.state & (1<< (Map[next.x][next.y]-' A ')))
Continue;
if (Map[next.x][next.y] >= ' A ' && Map[next.x][next.y] <= ' J ')
{
Next.state = Now.state | (1<< (map[next.x][next.y]-' a '));
Next.step = Now.step + 1;
if (Vis[next.x][next.y][next.state])
Continue;
Que.push (next);
Vis[next.x][next.y][next.state] = 1;
Continue
}
Next.state = now.state;
Next.step = Now.step + 1;
if (Vis[next.x][next.y][next.state])
Continue;
Vis[next.x][next.y][next.state] = 1;
Que.push (next);
}
}
}
int main ()
{
Freopen ("In.txt", "R", stdin);
int time;
while (scanf ("%d%d%d", &n, &m, &time)! = EOF)
{
for (int i = 1;i <= n;i++)
{
scanf ("%s", &map[i][1]);
for (int j = 1;j <= m;j++)
if (map[i][j] = = ' @ ')
st_x = I,st_y = j;
}
ans = inf; may not reach the end, so it is possible BFS did not update ans
BFS (); So ans always initialize it.
if (ans < time)
printf ("%d\n", ans);
Else
printf (" -1\n");
}
return 0;
}

















































































































































HDU 1429 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.