ZOJ 2655 Water Pipe BFS Belt direction State

Source: Internet
Author: User

Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2655

Water Pipe Time limit: 5 Seconds Memory Limit: 32768 KB

The waterworks want to connect to each of the water pipes. Just as the map shows, the waterworks sit on both corners of the city. The city was a rectangle, and is divided to a lot of small squares. Water pipes is placed in these squares. Only one pipe can is placed in each square. There is of types of pipe:straight pipe and bend pipe. You can rotate them if necessary. The types of pipe has different prices. You is assigned to calculate the minimum cost to connect the waterworks.

Input:

There is several test cases. Each test case contains the parts.

The first part was a line of integers:the width of the city, the length of the city, the price of the straight pipe, t He price of the bend pipe (1<=width,length<=100, 0<=price<=100).

The second part was a map of the city, and "B" stands for block in that square, while "*" stands for a blank square which you c A place water pipe in.

Output:

For each test case, print in one line the minimum cost to connect the waterworks. If can not be to connect to the other, output "impossible".

Sample Input:

1 2 3 4**5 5 8 0b***********bb***bb******5 5 8 10b***********bb***bb******1 1 89*1 1 3 3b

Sample Output:

887638impossible

Test instructions

From the upper right corner, proceed to the lower left corner. Take a straight pipe to spend the STR, bent Ben.

Ask for the minimum cost of plumbing.


Practice

directly from the top right corner of the map (1,n) point, the initial direction is left. Note that there is no water pipe at this time. The direction is due to flow from the left side of the waterworks so left.

Then it is in this lattice to build a water pipe, if built straight pipe, he continued to the left, and then spent the money of the straight pipe, reached (1,n-1) point, then the current direction or left, and then record the minimum number of steps in the VIS array.


Note that when walking, can only walk three directions, or turn, or straight away, can not go back, and the problem contradiction, there is no point.


And then when you get to the grid next to the finish line, the grid hasn't built a pipe, and if it's down, you can spend the money turning the pipe to get him to the finish line,

If left at this point, use only the money that costs the straight pipe to get him to the finish line.

If the other two directions, the answer cannot be updated.


.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 0x7f7f7f7fint dir[4][2]={1,0,0,1,-1,0,0,-1} ; struct Point{int x,y;int ff;//in the current position toward where int cost;bool operator < (const point& b) Const{return cost>b.cost;}}; int Vis[110][110][4];char Mp[110][110];int N,m;int VST (Point a) {if (a.x<1| | a.x>n| | a.y<1| | A.Y&GT;M) return 0;if (mp[a.x][a.y]== ' B ') return 0;if (vis[a.x][a.y][a.ff]>a.cost) {vis[a.x][a.y][a.ff]=a.cost; return 1;} return 0;} int str,ben;//Zhijia Wanjiaint ans;//The optimal path does not overlap with void bfs () {Priority_queue<point>q;memset (vis,inf,sizeof vis); Point Sta,nxt,nw;sta.x=1;sta.y=m;sta.ff=3;sta.cost=0;if (VST (STA)) Q.push (STA); Ans=inF;while (!q.empty ()) {nw=q.top ();//printf ("%d\n", Nw.cost), if (nw.x==n&&nw.y==1) {if (nw.ff==3) ans=min (ans, NW.COST+STR); else if (nw.ff==0) ans=min (Ans,nw.cost+ben);} Q.pop (); for (int i=-1;i<=1;i++) {int tur= (nw.ff+i+4)%4;nxt=nw;nxt.ff=tur;nxt.x+=dir[tur][0];nxt.y+=dir[tur][1]; if (i==0) nxt.cost+=str;elsenxt.cost+=ben;if (VST (NXT)) {Q.push (NXT);}}} }int Main () {while (scanf ("%d%d%d%d", &m,&n,&str,&ben)!=eof) {for (int i=1;i<=n;i++) scanf ("%s", mp[ i]+1); BFs (); if (ans!=inf) printf ("%d\n", ans); elseprintf ("impossible\n");} return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 2655 Water Pipe BFS Belt direction State

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.