Question 1644: [usac2007 Oct] obstacle course obstacle training course time limit: 5 sec memory limit: 64 MB
Description
Consider a square farm consisting of one square of N x n (1 <= n <= 100. Some squares cannot be set on by cows. They are marked as 'x '. For example:
... B x.
. X.
... X.
. X...
.. X ..
Bessie found herself at point A. She wanted to lick the salt at point B. Slow and clumsy animals, such as cows, hate turning. Even so, they still turn when necessary. For a given farm, calculate the minimum number of turns from A to B. At the beginning, Bessie could face any direction. Bessie knows she will be there.
Input
Row 1st: an integer of N rows
2. n + 1: The line I + 1 contains n characters ('.', 'x', 'A', 'B'), indicating the status of each vertex.
Output
Row 1: an integer with the minimum number of turns.
Sample input3
. XA
...
BX.
Sample output2
Hint Source
Silver
Question
This is a wide search, and the cost is + 1 only when turning. I feel a little bored and don't want to write Qaq.
Code
1 /*Author:WNJXYK*/ 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 using namespace std; 6 int T; 7 int n,m,w; 8 struct Edge{ 9 int v;10 int t;11 int nxt;12 Edge(){}13 Edge(int a,int b,int c){14 v=a;t=b;nxt=c;15 }16 };17 Edge e[6000];18 int nume;19 int head[510];20 21 inline void addSingleEdge(int x,int y,int w){22 e[++nume]=Edge(y,w,head[x]);23 head[x]=nume;24 }25 inline void addEdge(int x,int y,int w){26 addSingleEdge(x,y,w);27 addSingleEdge(y,x,w);28 }29 30 queue<int> que;31 int dist[510];32 bool inque[510];33 int intime[510];34 35 inline void solve(){36 bool isPrint=false;37 while(!que.empty()) que.pop();38 memset(dist,127/3,sizeof(dist));39 memset(inque,false,sizeof(inque));40 memset(intime,0,sizeof(intime));41 que.push(1);42 dist[1]=0;43 inque[1]=true;44 intime[1]++;45 while(!que.empty()){46 int now=que.front();47 que.pop();48 for (int i=head[now];i;i=e[i].nxt){49 int v=e[i].v;int w=e[i].t;50 if (dist[v]>dist[now]+w){51 intime[v]++;52 if (intime[v]>n){53 printf("YES\n");54 isPrint=true;55 break;56 }57 dist[v]=dist[now]+w;58 if (!inque[v]){59 inque[v]=true;60 que.push(v);61 }62 }63 }64 if (isPrint) break;65 inque[now]=false;66 }67 if (isPrint==false) printf("NO\n");68 }69 inline void read(){70 scanf("%d%d%d",&n,&m,&w);71 memset(head,0,sizeof(head));72 nume=0;73 for (int i=1;i<=m;i++){74 int x,y,t;75 scanf("%d%d%d",&x,&y,&t);76 addEdge(x,y,t);77 }78 for (int i=1;i<=w;i++){79 int x,y,t;80 scanf("%d%d%d",&x,&y,&t);81 addSingleEdge(x,y,-t);82 }83 }84 85 int main(){86 scanf("%d",&T);87 for (;T--;){88 read();89 solve();90 }91 return 0;92 }
View code
Bzoj 1644: [usac2007 Oct] obstacle course obstacle Training Course