HDU 3345 War Chess BFS

Source: Internet
Author: User

N * M graph.
'Y' is your current position (there is one and only one Y in the given map ).
'.' Is a normal grid. It costs you 1 MV to enter in this gird.
'T'is a tree. It costs you 2 MV to enter in this gird.
'R' is a river. It costs you 3 MV to enter in this gird.
'#' Is an obstacle. You can never enter in this gird.
'E' s are your enemies. you cannot move your SS your enemy, because once you enter the grids which are adjacent with 'e', you will lose all your MV. here "adjacent" means two grids share a common edge.
'P' are your partners. you can move your SS your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid. you can assume the Ps must stand on '. '. so, it also costs you 1 MV to enter this grid.
A graph and the total number of steps are given.
Then, output the walk-through graph, and mark the place where the walk is going.
A took over 10 hours. It was first done with DFS, marked visit [] [], and then traced back, but it went directly to TLE. Because visit [] [] marks only one step after all the paths are completed, after all the steps are traced back, which leads to many repeated steps.
For the second time, I changed BFS, but did not mark the array. Instead, I directly cracked the stack's T_T.
The third time is BFS, but a visit [] [] array is added to store the number of steps. In the search process, the number of steps for visit [] [] is updated every time you go to this step, this maximizes the number of remaining steps to the coordinates (x, y.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 2005
# Define inf 1 <28
Using namespace std;
 
Char Map1 [Max] [Max]; // source Image
Char Map2 [Max] [Max]; // output diagram
Int visit [Max] [Max]; // number of storage steps
Int n, m;
Int movex [4] = {1,-1, 0 };
Int movey [4] = {0, 0, 1,-1 };
Struct kdq
{
Int x, y, num;
};
Int inmap (int x, int y) // determines whether the vertex can arrive.
{
If (x <= 0 | y <= 0 | x> n | y> m | Map1 [x] [y] = '#' | Map1 [x] [y] = 'E' | Map1 [x] [y] = 'y ')
Return 0;
Return 1;
}
Int isEnemies (int x, int y) // determines whether E exists in four directions. If yes, stop.
{
Int I, j;
For (I = 0; I <4; I ++)
{
Int tx = x + movex [I];
Int ty = y + movey [I];
If (inmap (tx, ty) & Map1 [tx] [ty] = 'E ')
Return 1;
}
Return 0;
}
Int movesum (int x, int y, int move) // calculates the number of steps to this step and returns the number of remaining steps.
{
Int movenum, I;
If (Map1 [x] [y] = 'T ')
Movenum = move-2;
If (Map1 [x] [y] = 'R ')
Movenum = move-3;
If (Map1 [x] [y] = '.')
Movenum = move-1;
If (Map1 [x] [y] = 'P ')
Movenum = move-1;
For (I = 0; I <4; I ++) // determine whether E exists in the four directions after this step. If yes, use break;
{
Int tx = x + movex [I];
Int ty = y + movey [I];
If (tx> 0 & ty> 0 & tx <= n & ty <= m & Map1 [tx] [ty] = 'E ')
Break;
}
If (I! = 4) // I! = 4, that is, when the break comes out, it proves that E exists in the 1.4 directions, so it cannot be moved, that is, the number of remaining steps is 0.
{
If (movenum> 0)
Movenum = 0;
}
Return movenum; // number of returned steps
}
Void bfs (int x, int y, int move)
{
Kdq;
Int I, j;
For (I = 0; I <= n; I ++) // initialize visit [] [] to be infinitely small
For (j = 0; j <= m; j ++)
Visit [I] [j] =-inf;
A. x = x;
A. y = y;
A. num = move;
Queue <kdq> q;
Q. push ();
Visit [x] [y] = move; // initial steps
Int num = 0;
While (! Q. empty ())
{
Kdq temp = q. front ();
Q. pop ();
If (temp. num = 0) // if the number of steps is 0, continue directly;
Continue;
If (num) if (isEnemies (temp. x, temp. y) continue; // If E exists in the four directions of the initial position, Y can be moved, so a num is used to indicate whether this step is the first step.
// If it is not the first step and there is E in the four directions, it cannot be moved. Continue;
Num ++; // indicates whether it is the first step of moving.
For (int I = 0; I <4; I ++)
{
Int tx = temp. x + movex [I];
Int ty = temp. y + movey [I];
If (inmap (tx, ty ))
{
Kdq now;
Now. num = movesum (tx, ty, temp. num); // calculate the remaining steps
// If (Map1 [tx] [ty] = 'P' & now. num <1) continue;
If (now. num <0) continue;
If (now. num> visit [tx] [ty]) // update visit [] []
{
Visit [tx] [ty] = now. num;
Now. x = tx;
Now. y = ty;
Q. push (now );
If (Map1 [tx] [ty]! = 'P ')
Map2 [tx] [ty] = '*'; // construct Map2 [] []
}
 
}
}
}
}
Int main ()
{
Int I, j, l, k, T, s;
Int numx, numy;
Cin> T;
While (T --)
{
Cin> n> m> s;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= m; j ++)
{
Cin> Map1 [I] [j];
Map2 [I] [j] = Map1 [I] [j];
If (Map1 [I] [j] = 'y') // locate the initial Coordinate
{
Numx = I;
Numy = j;
}
}
}
Bfs (numx, numy, s );
For (I = 1; I <= n; I ++)
{Www.2cto.com
For (j = 1; j <= m; j ++)
Cout <Map2 [I] [j];
Cout <endl;
}
Cout <endl;
}
Return 0;
}

Author: kdqzzxxcc

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.