Trip to Zhongzhou Time limit (Common/Java): 1000 MS/3000 MS running memory limit: 65536 Kbyte Total submission: 27 tested: 12 Description In order to celebrate the end of the fight for the sky, Yb, CJ and zjn decided to travel to ZhongZhou. But it will go through the terrible black corner, The black corner is composed of N * n square grids, with the upper left corner as the starting point ◎, the coordinate is (), the X axis is right, the Y axis is downward, and the side length of each square is 1. As shown in, a three-person car departs from the starting point ◎ and goes to the center-state entrance at the bottom right corner. The coordinates are (N, N ). There are gas stations (represented by ●) at several grid intersections to provide fuel for a car while driving. There are various rules in the black corner domain, and a slight carelessness will lead to death. Although Yb's martial arts are strong, Yb decided to comply with these rules for security considerations of CJ and zjn. The following rules should be observed when the vehicle is driving in the black corner: (1) A car can only drive along the grid side. After full of oil, it can drive K grid sides. The car was full of gasoline at departure and there was no gas station at the start and end points. (2) If the X or Y coordinates of an automobile go through a grid edge are reduced, B is charged. Otherwise, no fee is charged. (3) If a car encounters a gas station while driving, it must be filled with oil and a fixed fuel filling fee will be paid. (4) You can add a gas station at any grid point as needed, and pay C for the gas station (excluding fuel ). (5) (1 )~ (4) The numbers N, K, A, B, and C in are all positive integers and meet the following constraints: 2 <= n <= 100,2 <= k <= 10, 1 <=, b, C <= 150. Due to the poor stock market recently, the financial resources of the three people are very tight, so they hope to take the path with the lowest cost. Input Single Group Test Data The first line is the value of N, K, A, B, and C. Starting from the second row is a 0-1 matrix of N * n, with N values in each row ending with n + 1. When the value of column J in row I of the phalanx is 1, a gas station is set at the grid intersection (I, j). If it is 0, no gas station is set. The numbers of adjacent rows are separated by spaces. Output Minimum cost from the start point to the end point Sample Input 3 1 1 2 3 0 0 0 0 0 0 0 0 0 9 3 2 3 6 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 Sample output 12 12 Question Source Yb |
Question address:
Http: // 218.194.91.48/acmhome/problemdetail. do? & Method = showdetail & id = 1076
Good search practice questions.
The first time I did this, I kept Wa and did not come out.
After a long period of N, I did it again, or Wa. Fortunately, after three hours, I finally found the error by calculating the data step by step.
... I used maps to record the status. This status is updated when the element before the result queue is passed. While the queue elements in the same status follow the new status when they move again.
... This error is hard to find... In the future, when you encounter an error, you cannot just check it.CodeAndAlgorithm...
# Include <iostream> # include <cstdio> # include <queue> # include <cstring> # include <algorithm> using namespace STD; int N, K, A, B, C; int map [110] [110]; int step [110] [110] [12]; // if the remaining K steps are different, they cannot be overwritten. In the past, this was the error, this error is still found for 3 hours. Remember, struct point {int X, Y, K;} que [440000]; int FX [] [2] = {-}, {0,-1}, {}, {}; int BFS () {int I, j, s, T, nstep, MX; point now, next; memset (step,-1, sizeof (STEP); now. X = 0, now. y = 0, now. k = K; que [S = T = 0] = now; Step [0] [0] [k] = 0; n --; C + = A; MX = 100000000; while (S <= T) {now = que [S % 400000]; s ++; for (I = 0; I <4; I ++) {next. X = now. X + FX [I] [0]; next. y = now. Y + FX [I] [1]; If (next. x <0 | next. x> N | next. Y <0 | next. y> N) continue; nstep = step [now. x] [now. y] [now. k]; If (next. X = N & next. y = N) MX = Mx <nstep? MX: nstep; if (I <2) nstep + = B; If (Map [next. x] [next. y]) {nstep + = A; next. k = K;} else {next. k = now. k-1; If (next. k <= 0) {next. k = K; nstep + = C ;}} if (next. k = 0) cout <"1" <Endl; If (step [next. x] [next. y] [next. k]! =-1) if (nstep> = step [next. x] [next. y] [next. k]) continue; step [next. x] [next. y] [next. k] = nstep; t ++; que [T % 400000] = next ;}} return MX ;}int main () {int I, j; while (~ Scanf ("% d", & N, & K, & A, & B, & C) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) scanf ("% d", & map [I] [J]); printf ("% d \ n", BFs ();} return 0 ;}