题解:P4621 [COCI 2012/2013 #6] BAKTERIJE
CatFromMars · · 题解
恶臭细节题。
这道题看上去完全不可做,定睛一看
对于它到点
代码细节很恶臭,调了很久。如果大家有细节错误可以到这里下载数据 qwq。
#include <bits/stdc++.h>
#define int long long
#define ll long long
using namespace std;
int cdiv(int n, int m) {
return ((n % m == 0) ? (n / m) : (n / m + 1));
}
const ll inf = 9e18;
const int N = 50;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
struct node {
int x, y, dir;
};
int a[6][N + 10][N + 10];
int n, m, qk, X[N + 10], Y[N + 10], S[N + 10], edx, edy;
bool vis[N + 10][N + 10][5];
int step[6][N + 10][N + 10][5];
vector <node> vec[10];
int start[10], tim[10], c[10][10];//type[i] i->repeat
void getpath(int x, int y, int w, int ind) {
int nowx = x, nowy = y, nowd = w, st = 0;
while(1) {
vis[nowx][nowy][nowd] = 1;
step[ind][nowx][nowy][nowd] = st; st++;
vec[ind].push_back((node){nowx, nowy, nowd});
if(nowx == edx && nowy == edy)
c[ind][nowd] = st - 1;
nowd = (nowd + a[ind][nowx][nowy]) % 4;
int xx = nowx + dx[nowd], yy = nowy + dy[nowd];
if(!(xx >= 1 && xx <= n && yy >= 1 && yy <= m)) {
if(nowd == 0) nowd = 2;
else if(nowd == 2) nowd = 0;
else if(nowd == 1) nowd = 3;
else if(nowd == 3) nowd = 1;
xx = nowx + dx[nowd], yy = nowy + dy[nowd];
}
if(vis[xx][yy][nowd]) {
start[ind] = step[ind][xx][yy][nowd];
tim[ind] = vec[ind].size() - start[ind];
break;
}
else {
nowx = nowx + dx[nowd];
nowy = nowy + dy[nowd];
}
}
for(int i = 0; i < vec[ind].size(); i++)
vis[vec[ind][i].x][vec[ind][i].y][vec[ind][i].dir] = 0;
}
int arrd[10];
ll ans = 9e18;
namespace excrt {
int exgcd(int a, int b, int &x, int &y) {
if(!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, x, y);
int tmp = x; x = y;
y = tmp - a / b * y;
return d;
}
int qmul(int n, int m, int Mod) {
int res = 0;
while(m) {
if(m & 1) res = (res + n) % Mod;
n = (n + n) % Mod, m >>= 1;
}
return res;
}
int d;
int xxtyfc(int a, int b, int c) {
int x, y;
d = exgcd(a, c, x, y);
if(b % d != 0) return -1;
x = qmul(x, b / d, c);
y = qmul(y, b / d, c);
return (x % (c / d) + (c / d)) % (c / d);
}
int r[N + 10], m[N + 10];
int excrt() {
int x = xxtyfc(1, r[1], m[1]);
int L = m[1];
int maxn = 0;
for(int i = 1; i <= qk; i++)
maxn = max(maxn, r[i]);
for(int i = 2; i <= qk; i++) {
int t = xxtyfc(L, ((r[i] - x + m[i]) % m[i] + m[i]) % m[i], m[i]);
if(t == -1) {
return inf;
}
int tmp = L;
L = L / d * m[i];
x = (x + qmul(t, tmp, L)) % L;
}
x %= L;//特别的 x 要比所有的 t 大!
while(x < maxn)
x += L;
return x;
}
}
void calc(int u) {//搜索
if(u > qk) {
int cnt = 0;
for(int i = 1; i <= qk; i++)
if(c[i][arrd[i]] == -1) return ;
for(int i = 1; i <= qk; i++)
if(c[i][arrd[i]] < start[i])
cnt++;
if(cnt) {//存在链上可以直接判断
ll stand = -1;
bool nos = 0;
for(int i = 1; i <= qk; i++)//在链上的讨论
if(c[i][arrd[i]] < start[i]) {
if(stand < 0) stand = c[i][arrd[i]];
else if(c[i][arrd[i]] != stand) {
nos = 1;
break;
}
}
for(int i = 1; i <= qk; i++)//在环上的讨论
if(c[i][arrd[i]] >= start[i]) {
if(c[i][arrd[i]] > stand) {
nos = 1;
break;
}
else if((stand - c[i][arrd[i]]) % tim[i] != 0) {
nos = 1;
break;
}
}
if(!nos) ans = min(ans, stand);
}
else {
for(int i = 1; i <= qk; i++)
excrt::r[i] = c[i][arrd[i]], excrt::m[i] = tim[i];//否则可以直接 excrt
ans = min(ans, excrt::excrt());
}
return ;
}
for(int i = 0; i < 4; i++)
if(c[u][i] != -1)
arrd[u] = i, calc(u + 1);
}
signed main() {
memset(c, -1, sizeof(c));
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m >> qk;
cin >> edx >> edy;
for(int i = 1; i <= qk; i++) {
char o;
cin >> X[i] >> Y[i] >> o;
if(o == 'R') S[i] = 0;
else if(o == 'D') S[i] = 1;
else if(o == 'L') S[i] = 2;
else S[i] = 3;
for(int j = 0; j <= n + 1; j++)
for(int k = 0; k <= m + 1; k++)
a[i][j][k] = 2;
for(int j = 1; j <= n; j++) {
for(int k = 1; k <= m; k++) {
char ch; cin >> o;
a[i][j][k] = (o - '0') % 4;
}
}
}
for(int i = 1; i <= qk; i++)
getpath(X[i], Y[i], S[i], i);
calc(1);
if(ans >= inf) cout << -1 << endl;
else cout << ans + 1 << endl;
}