题目链接:https://vjudge.net/problem/UVA-10798
题目含义:
这个题目要求的就是走到出口的一条路线,然后分别再旋转90,180,270度,形成共4条线路,求min(max(4条线路每条线路踩到的花)))
AC代码
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline bool scan_d (T &ret) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0; //EOF
while (c != '-' && (c < '0' || c > '9') ) {
if((c = getchar()) == EOF) return 0;
}
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template<typename T>
void print(T x) {
static char s[33], *s1; s1 = s;
if (!x) *s1++ = '0';
if (x < 0) putchar('-'), x = -x;
while(x) *s1++ = (x % 10 + '0'), x /= 10;
while(s1-- != s) putchar(*s1);
}
template<typename T>
void println(T x) {
print(x); putchar('\n');
}
#define maxx 600
char strs[23][23];
char pp[200];
int fac[4]={1,11,121,1331};
int vis[23][23][500000];
struct node{
int s;
int e;
int a[4];
int step;
friend bool operator <(const node &a,const node &b)
{
return a.step>b.step;
}
};
int n;
int dir[][2]={{-1,0},{1,0},{0,-1},{0,1}};
int bfs()
{
memset(vis,0,sizeof(vis));
priority_queue<node>pq;
node n1,n2;
n1.s=n/2+1;
n1.e=n1.s;
n1.a[0]=n1.a[1]=n1.a[2]=n1.a[3]=0;
n1.step=0;
pq.push(n1);
vis[n1.s][n1.e][0]=1;
while(!pq.empty())
{
n1=pq.top();
pq.pop();
for(int i=0;i<4;++i)
{
n2.s=n1.s+dir[i][0];
n2.e=n1.e+dir[i][1];
if(n2.s<1||n2.s>n||n2.e<1||n2.e>n)
{
return n1.step;
}
else
{
n2.a[0]=n1.a[0]+pp[strs[n2.s][n2.e]];
n2.a[1]=n1.a[1]+pp[strs[n+1-n2.e][n2.s]];
n2.a[2]=n1.a[2]+pp[strs[n+1-n2.s][n+1-n2.e]];
n2.a[3]=n1.a[3]+pp[strs[n2.e][n+1-n2.s]];
int h=0;
for(int i=0;i<4;++i)
h+=n2.a[i]*fac[i];
if(vis[n2.s][n2.e][h])
continue;
vis[n2.s][n2.e][h]=1;
int temp=0;
for(int i=0;i<4;++i)
temp=max(temp,n2.a[i]);
n2.step=temp;
pq.push(n2);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
pp['P']=0;
pp['.']=0;
pp['R']=1;
while(scan_d<int>(n)&&n)
{
for(int i=1;i<=n;++i)
{
scanf("%s",strs[i]+1);
}
int ans=bfs();
printf("At most %d rose(s) trampled.\n",ans);
}
}