text
stringlengths
49
983k
#include<iostream> #include<cmath> #include<vector> #include<string> #include<numeric> #include<algorithm> using namespace std; int par[123]; int find(int x){ return (par[x]==x)?x:par[x]=find(par[x]); } void unite(int a,int b){ par[find(a)]=find(b); } int ex[123][1234],ey[123][1234]; int evx[123][1234],evy[123][1234]; double sqr(double x){return x*x;} int main(){ for(int N,T,R;cin>>N>>T>>R,N|T|R;){ char nicknames[123][12]; vector<int> t[123],x[123],y[123],vx[123],vy[123]; vector<double> ev; for(int i=0;i<N;i++){ cin>>nicknames[i]; for(int j=0;;j++){ int ti,xi,yi; cin>>ti>>xi>>yi; ev.push_back(ti); t[i].push_back(ti); int cx,cy; if(j){ vx[i].push_back(xi); vy[i].push_back(yi); for(int k=t[i][j-1]+1;k<=ti;k++){ ey[i][k]=ey[i][k-1]+yi; ex[i][k]=ex[i][k-1]+xi; evy[i][k-1]=yi; evx[i][k-1]=xi; } cy=ey[i][ti]; cx=ex[i][ti]; }else{ cx=xi; cy=yi; ex[i][0]=cx; ey[i][0]=cy; } x[i].push_back(cx); y[i].push_back(cy); if(ti==T)break; } } for(int i=0;i<N;i++){ for(int j=0;j<vx[i].size();j++){ int y1=y[i][j]-t[i][j]*vy[i][j]; int x1=x[i][j]-t[i][j]*vx[i][j]; for(int k=0;k<i;k++){ for(int l=0;l<vx[k].size();l++){ int y2=y[k][l]-t[k][l]*vy[k][l]; int x2=x[k][l]-t[k][l]*vx[k][l]; long long X=x1-x2; long long Y=y1-y2; long long VX=vx[i][j]-vx[k][l]; long long VY=vy[i][j]-vy[k][l]; long long a=VX*VX+VY*VY; long long b=X*VX+Y*VY; long long c=X*X+Y*Y-R*R; long long inr=b*b-a*c; if(inr>=0&&a){ ev.push_back((-b-sqrt(inr))/a); ev.push_back((-b+sqrt(inr))/a); } } } } } sort(begin(ev),end(ev)); bool has[123]={true}; for(int k=0;k<ev.size()-1;k++){ double e=(ev[k]+ev[k+1])/2; // cerr<<e<<endl; if(e<0||T<e)continue; iota(begin(par),end(par),0); int g=e; double f=e-g; for(int i=0;i<N;i++){ for(int j=0;j<i;j++){ if(sqr(ey[i][g]+evy[i][g]*f-(ey[j][g]+evy[j][g]*f))+ sqr(ex[i][g]+evx[i][g]*f-(ex[j][g]+evx[j][g]*f))<=R*R){ unite(i,j); } } } for(int i=0;i<N;i++){ has[find(i)]|=has[i]; } for(int i=0;i<N;i++){ has[i]|=has[find(i)]; } } vector<string> ans; for(int i=0;i<N;i++){ if(has[i]){ ans.push_back(nicknames[i]); } } sort(begin(ans),end(ans)); for(auto e:ans){ cout<<e<<endl; } } }
#include <algorithm> #include <cmath> #include <cstdlib> #include <functional> #include <queue> #include <iostream> #include <tuple> #include <vector> using namespace std; #define REP(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep(i, n) REP(i, 0, n) #define ALL(v) begin(v), end(v) constexpr double EPS = 1e-8; struct point { double x, y; point(double x_, double y_):x(x_), y(y_) {} point operator+(const point &p) const { return point(x + p.x, y + p.y); } point operator-(const point &p) const { return point(x - p.x, y - p.y); } point operator*(double d) const { return point(x * d, y * d); } }; double norm(const point &p) { return p.x * p.x + p.y * p.y; } double dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } typedef tuple<double, int, int> event; // time, idx1, idx2 typedef priority_queue<event, vector<event>, greater<event>> event_queue; // ax^2 + bx + c = 0 vector<double> solve_quadratic_equation(double a, double b, double c) { if(abs(a) <= EPS) { if(abs(b) <= EPS) return vector<double>(); return {- c / b}; } const double d = b * b - 4 * a * c; if(d < -EPS) return vector<double>(); return {(-b - sqrt(d)) / (2 * a), (-b + sqrt(d)) / (2 * a)}; } vector<double> get_times(const point &a, const point &va, const point &b, const point &vb, int r) { const point d_p = a - b; const point d_v = va - vb; double l = norm(d_v); double m = 2 * dot(d_p, d_v); double n = norm(d_p) - r * r; return solve_quadratic_equation(l, m, n); } vector<double> calculate(const vector<int> &ta, const vector<point> &va, const vector<int> &tb, const vector<point> &vb, int r) { const int n = ta.size(); const int m = tb.size(); point a = va[0]; point b = vb[0]; vector<double> res; if(norm(a - b) <= r * r) res.emplace_back(0); int i = 0, j = 0; while(true) { const int start = max(ta[i], tb[j]); const int finish = min(ta[i + 1], tb[j + 1]); if(start < finish) { const auto times = get_times(a + va[i + 1] * (start - ta[i]), va[i + 1], b + vb[j + 1] * (start - tb[j]), vb[j + 1], r); for(const double &t : times) { if(EPS <= t && t <= finish - start) res.emplace_back(t + start); } } if(finish == ta[i + 1]) a = a + va[i + 1] * (ta[i + 1] - ta[i]), ++i; if(finish == tb[j + 1]) b = b + vb[j + 1] * (tb[j + 1] - tb[j]), ++j; if(i == n - 1 || j == m - 1) break; } return res; } void update_know(vector<bool> &know, const vector<vector<bool>> &mat) { const int n = know.size(); queue<int> que; rep(i, n) { if(know[i]) que.push(i); } while(!que.empty()) { const int v = que.front(); que.pop(); rep(i, n) { if(mat[v][i] && !know[i]) { que.push(i); know[i] = true; } } } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for(int n, T, r; cin >> n >> T >> r && n;) { vector<string> names(n); vector<vector<int>> t(n); vector<vector<point>> vec(n); rep(i, n) { cin >> names[i]; while(true) { int time, x, y; cin >> time >> x >> y; t[i].emplace_back(time); vec[i].emplace_back(x, y); if(time == T) break; } } event_queue que; rep(i, n) { rep(j, i) { const auto times = calculate(t[i], vec[i], t[j], vec[j], r); for(const auto &e : times) { que.push(make_tuple(e, j, i)); } } } vector<vector<bool>> mat(n, vector<bool>(n, false)); vector<bool> know(n, false); know[0] = true; while(!que.empty()) { const int v = get<1>(que.top()); const int u = get<2>(que.top()); que.pop(); mat[v][u] = mat[u][v] = !mat[v][u]; if(mat[v][u]) update_know(know, mat); } vector<string> know_robots; rep(i, n) { if(know[i]) know_robots.emplace_back(names[i]); } sort(ALL(know_robots)); for(const auto &e : know_robots) { cout << e << endl; } } return EXIT_SUCCESS; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <queue> #include <utility> #include <complex> #include <cmath> using namespace std; #define EPS 1e-9 typedef complex<int> P; typedef const P &rP; double dot(rP a, rP b){ return real(a) * real(b) + imag(a) * imag(b); } struct event{ double tm; int a, b; bool operator< (const event &e) const{ return tm < e.tm; } }; struct robot{ string name; vector<double> tm; vector<P> v; }; vector<event> evs; void approach(int R, int p, int q, P dp, P dv, double t0, double t1){ vector<double> ret; double dt = t1 - t0; double a = norm(dv); double b = dot(dv, dp); double c = norm(dp) - R * R; double D = b * b - a * c; if(D > 0.0){ D = sqrt(D); double t = (-b - D) / a; if(EPS < t && t <= dt){ evs.push_back((event){t + t0, p, q}); } t = (-b + D) / a; if(EPS < t && t <= dt){ evs.push_back((event){t + t0, p, q}); } } } void enumevent(int R, int p, int q, const robot &r1, const robot &r2){ int k1 = 1, k2 = 1; int tp = 0; P p1 = r1.v[0], p2 = r2.v[0]; if(norm(p2 - p1) <= R * R){ evs.push_back((event){0.0, p, q}); } while(k1 < r1.v.size() && k2 < r2.v.size()){ int tn = min(r1.tm[k1], r2.tm[k2]); approach(R, p, q, p2 - p1, r2.v[k2] - r1.v[k1], tp, tn); p1 += r1.v[k1] * (tn - tp); p2 += r2.v[k2] * (tn - tp); if(r1.tm[k1] == tn){ ++k1; } if(r2.tm[k2] == tn){ ++k2; } tp = tn; } } int main(){ int n, k, x, y, T, R, t; while(cin >> n >> T >> R, n){ evs.clear(); vector<robot> rbs(n); for(int i = 0; i < n; ++i){ cin >> rbs[i].name; do{ cin >> t >> x >> y; rbs[i].tm.push_back(t); rbs[i].v.push_back(P(x, y)); } while(t != T); } for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j){ enumevent(R, i, j, rbs[i], rbs[j]); } sort(evs.begin(), evs.end()); vector<vector<char> > con(n, vector<char>(n)); vector<char> has(n); has[0] = 1; for(int i = 0; i < evs.size(); ++i){ int a = evs[i].a, b = evs[i].b; if(con[a][b]){ con[a][b] = con[b][a] = 0; } else{ con[a][b] = con[b][a] = 1; queue<int> q; if(has[a]){ q.push(a); } if(has[b]){ q.push(b); } while(!q.empty()){ for(int j = 0; j < n; ++j){ if(!has[j] && con[q.front()][j]){ has[j] = 1; q.push(j); } } q.pop(); } } } vector<string> ans; ans.reserve(n); for(int i = 0; i < n; ++i){ if(has[i]){ ans.push_back(rbs[i].name); } } sort(ans.begin(), ans.end()); for(int i = 0; i < ans.size(); ++i){ cout << ans[i] << '\n'; } } }
#include<stdio.h> #include<algorithm> #include<math.h> #include<vector> #include<string> #include<queue> using namespace std; const double EPS = 1e-10; const double INF = 1e+10; const double PI = acos(-1); int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } inline double ABS(double a){return max(a,-a);} struct Pt { double x, y; Pt() {} Pt(double x, double y) : x(x), y(y) {} Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); } Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); } Pt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); } Pt operator-() const { return Pt(-x, -y); } Pt operator*(const double &k) const { return Pt(x * k, y * k); } Pt operator/(const double &k) const { return Pt(x / k, y / k); } double ABS() const { return sqrt(x * x + y * y); } double abs2() const { return x * x + y * y; } double arg() const { return atan2(y, x); } double dot(const Pt &a) const { return x * a.x + y * a.y; } double det(const Pt &a) const { return x * a.y - y * a.x; } }; double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); } int iSP(Pt a, Pt b, Pt c) { int s = sig((b - a).det(c - a)); if (s) return s; if (sig((b - a).dot(c - a)) < 0) return -2; // c-a-b if (sig((a - b).dot(c - b)) < 0) return +2; // a-b-c return 0; } double dSP(Pt a, Pt b, Pt c) { if (sig((b - a).dot(c - a)) <= 0) return (c - a).ABS(); if (sig((a - b).dot(c - b)) <= 0) return (c - b).ABS(); return ABS(tri(a, b, c)) / (b - a).ABS(); } bool iCS(Pt a, double r, Pt b, Pt c) { return (sig(r - dSP(b, c, a)) >= 0 && sig(r - max((b - a).ABS(), (c - a).ABS())) <= 0); } pair<Pt,Pt> pCL(Pt a, double r, Pt b, Pt c) { Pt h = b + (c - b) * (c - b).dot(a - b) / (c - b).abs2(); // perp(b, c, a) double d = (h - a).ABS(); double y = sqrt(max(r * r - d * d, 0.0)); Pt e = (c - b) / (c - b).ABS(); return make_pair(h - e * y, h + e * y); } char in[100]; string name[110]; vector<Pt>v[110]; vector<double>t[110]; vector<pair<double,pair<int,int> > >ev; vector<string>ans; int ret[110]; int g[110][110]; int bfs[110]; int main(){ int a,b,c; scanf("%d%d%d",&a,&b,&c); scanf("%s",in); int na,nb,nc; while(a){ double r=(double)c; for(int i=0;i<110;i++){ v[i].clear(); t[i].clear(); } for(int i=0;i<a;i++){ name[i]=in; while(1){ int chk=scanf("%s",in); if(chk==-1)break; if('a'<=in[0]&&in[0]<='z')break; int tmp,x,y; sscanf(in,"%d",&tmp); na=tmp; t[i].push_back((double)tmp); scanf("%d%d",&x,&y); nb=x;nc=y; v[i].push_back(Pt((double)x,(double)y)); } if(i==a-1){ t[i].pop_back(); v[i].pop_back(); } } ev.clear(); for(int i=0;i<a;i++)for(int j=i+1;j<a;j++){ if((v[i][0]-v[j][0]).ABS()<r)ev.push_back(make_pair(0,make_pair(i,j))); int L=1; int R=1; Pt at=v[i][0]-v[j][0]; while(1){ double left=max(t[i][L-1],t[j][R-1]); double right=min(t[i][L],t[j][R]); Pt tv=v[i][L]-v[j][R]; if(iCS(Pt(0,0),r,at,at+tv*(right-left))){ pair<Pt,Pt> it=pCL(Pt(0,0),r,at,at+tv*(right-left)); if(iSP(at,it.first,at+tv*(right-left))==2||(at+tv*(right-left)-it.first).ABS()<EPS){ ev.push_back(make_pair(left+(it.first-at).ABS()/(tv.ABS()),make_pair(i,j))); } if(iSP(at,it.second,at+tv*(right-left))==2||(at+tv*(right-left)-it.second).ABS()<EPS){ ev.push_back(make_pair(left+(it.second-at).ABS()/(tv.ABS()),make_pair(i,j))); } } at=at+tv*(right-left); if(L==t[i].size()-1&&R==t[j].size()-1)break; else if(L==t[i].size()-1)R++; else if(R==t[j].size()-1)L++; else if(t[i][L]>t[j][R]+EPS)R++; else if(t[i][L]+EPS<t[j][R])L++; else {L++;R++;} } } std::sort(ev.begin(),ev.end()); for(int i=0;i<a;i++)ret[i]=0; ret[0]=1; for(int i=0;i<a;i++)for(int j=0;j<a;j++)g[i][j]=0; for(int i=0;i<ev.size();i++){ g[ev[i].second.first][ev[i].second.second]^=1; if(g[ev[i].second.first][ev[i].second.second]&&ret[ev[i].second.first]+ret[ev[i].second.second]){ queue<int>Q; for(int j=0;j<a;j++)bfs[j]=0; Q.push(ev[i].second.first); bfs[ev[i].second.first]=1; while(Q.size()){ int at=Q.front(); Q.pop(); for(int j=0;j<a;j++){ if(!bfs[j]&&(g[at][j]||g[j][at])){ bfs[j]=1;Q.push(j); } } } for(int j=0;j<a;j++)if(bfs[j])ret[j]=1; } } ans.clear(); for(int i=0;i<a;i++)if(ret[i])ans.push_back(name[i]); std::sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++)printf("%s\n",ans[i].c_str()); a=na;b=nb;c=nc; } }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(a) (a).begin(),(a).end() #define EQ(a,b) (abs((a)-(b)) < EPS) using namespace std; typedef long double D; typedef complex<D> P; typedef pair<int,int> pii; typedef pair<D, pii> data; typedef vector<int> vi; const D EPS = 1e-10; const D PI = acos(-1); const D INF = 1e15; int n; string name[110]; D T,r,x[110],y[110],t[110][1100]; D vx[110][1100], vy[110][1100]; vector<D> cal(int a, int b){ vector<D> res; P pa = P(x[a],y[a]), pb = P(x[b],y[b]); if( abs(pa-pb)<r+EPS )res.push_back(0); int ta=1, tb=1; D cur = 0, nxt = min(t[a][ta],t[b][tb]); while(!EQ(cur,T)){ D tx = pa.real() - pb.real(), ty = pa.imag() - pb.imag(); D tvx = vx[a][ta] - vx[b][tb], tvy = vy[a][ta] - vy[b][tb]; D A = tvx*tvx + tvy*tvy, B = 2*tvx*tx + 2*tvy*ty, C = tx*tx + ty*ty - r*r; D d = B*B - 4*A*C; vector<D> sol; if(EQ(A,0)){ if(!EQ(B,0))sol.push_back(-C/B); }else if(EQ(d,0)){ sol.push_back(-B/2/A); }else if(d>EPS){ sol.push_back( (-B - sqrt(d))/2/A ); sol.push_back( (-B + sqrt(d))/2/A ); } rep(i,sol.size()){ if(sol[i]+EPS < 0 || nxt-cur+EPS < sol[i])continue; res.push_back(cur + sol[i]); } D dif = nxt - cur; pa += P(vx[a][ta]*dif, vy[a][ta]*dif); pb += P(vx[b][tb]*dif, vy[b][tb]*dif); cur = nxt; if(EQ(t[a][ta],cur))ta++; if(EQ(t[b][tb],cur))tb++; nxt = min(t[a][ta],t[b][tb]); } return res; } bool prop[110]; void dfs(int u, const vector<vi> &g){ prop[u] = true; rep(i,g[u].size()){ if(!prop[g[u][i]])dfs(g[u][i],g); } } int main(){ cin.tie(0); ios::sync_with_stdio(0); while(cin >> n >> T >> r){ if(n==0)break; rep(i,n){ cin >> name[i]; cin >> t[i][0] >> x[i] >> y[i]; for(int j=0;;j++){ cin >> t[i][j+1] >> vx[i][j+1] >> vy[i][j+1]; if(EQ(t[i][j+1], T))break; } } vector<data> event; rep(i,n)rep(j,i){ vector<D> sub_event = cal(i,j); for(D d : sub_event){ event.push_back(data(d, pii(i,j))); } } sort(all(event)); memset(prop,0,sizeof(prop)); prop[0] = true; vector<vi> graph(n,vi()); rep(i,event.size()){ if(event[i].first>T+EPS)break; int a = event[i].second.first, b = event[i].second.second; bool f = true; rep(j,graph[a].size()){ if(graph[a][j] == b){ f = false; break; } } if(f){ graph[a].push_back(b); graph[b].push_back(a); prop[a] |= prop[b]; if(prop[a])dfs(a,graph); }else{ rep(j,graph[a].size()){ if(graph[a][j] == b)graph[a].erase(graph[a].begin()+j); } rep(j,graph[b].size()){ if(graph[b][j] == a)graph[b].erase(graph[b].begin()+j); } } } vector<string> ans; rep(i,n){ if(prop[i])ans.push_back(name[i]); } sort(all(ans)); for(string s : ans)cout << s << endl; } }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(a) (a).begin(),(a).end() #define EQ(a,b) (abs((a)-(b)) < EPS) using namespace std; typedef double D; typedef complex<D> P; typedef pair<int,int> pii; typedef pair<D, pii> data; typedef vector<int> vi; const D EPS = 1e-8; int n; string name[110]; D T,r,x[110],y[110],t[110][1100]; D vx[110][1100], vy[110][1100]; vector<D> cal(int a, int b){ vector<D> res; P pa = P(x[a],y[a]), pb = P(x[b],y[b]); if( abs(pa-pb)<r+EPS )res.push_back(0); int ta=1, tb=1; D cur = 0, nxt = min(t[a][ta],t[b][tb]); while(!EQ(cur,T)){ D tx = pa.real() - pb.real(), ty = pa.imag() - pb.imag(); D tvx = vx[a][ta] - vx[b][tb], tvy = vy[a][ta] - vy[b][tb]; D A = tvx*tvx + tvy*tvy, B = 2*tvx*tx + 2*tvy*ty, C = tx*tx + ty*ty - r*r; D d = B*B - 4*A*C; vector<D> sol; if(EQ(A,0)){ if(!EQ(B,0))sol.push_back(-C/B); }else if(EQ(d,0)){ sol.push_back(-B/2/A); }else if(d>EPS){ sol.push_back( (-B - sqrt(d))/2/A ); sol.push_back( (-B + sqrt(d))/2/A ); } rep(i,sol.size()){ if(sol[i]+EPS < 0 || nxt-cur+EPS < sol[i])continue; res.push_back(cur + sol[i]); } D dif = nxt - cur; pa += P(vx[a][ta]*dif, vy[a][ta]*dif); pb += P(vx[b][tb]*dif, vy[b][tb]*dif); cur = nxt; if(EQ(t[a][ta],cur))ta++; if(EQ(t[b][tb],cur))tb++; nxt = min(t[a][ta],t[b][tb]); } return res; } bool prop[110]; vector<vi> g; void dfs(int v){ prop[v] = true; for(int u : g[v]){ if(!prop[u])dfs(u); } } int main(){ cin.tie(0); ios::sync_with_stdio(0); while(cin >> n >> T >> r){ if(n==0)break; rep(i,n){ cin >> name[i]; cin >> t[i][0] >> x[i] >> y[i]; for(int j=0;;j++){ cin >> t[i][j+1] >> vx[i][j+1] >> vy[i][j+1]; if(EQ(t[i][j+1], T))break; } } vector<data> event; rep(i,n)rep(j,i){ vector<D> sub_event = cal(i,j); for(D d : sub_event){ event.push_back(data(d, pii(i,j))); } } sort(all(event)); memset(prop,0,sizeof(prop)); prop[0] = true; g = vector<vi>(n,vi()); rep(i,event.size()){ if(event[i].first>T+EPS)break; int a = event[i].second.first, b = event[i].second.second; bool f = true; rep(j,g[a].size()){ if(g[a][j] == b){ f = false; break; } } if(f){ g[a].push_back(b); g[b].push_back(a); if(prop[a] |= prop[b])dfs(a); }else{ rep(j,g[a].size()){ if(g[a][j] == b)g[a].erase(g[a].begin()+j); } rep(j,g[b].size()){ if(g[b][j] == a)g[b].erase(g[b].begin()+j); } } } vector<string> ans; rep(i,n){ if(prop[i])ans.push_back(name[i]); } sort(all(ans)); for(string s : ans)cout << s << endl; } }
//150 #include<cstdio> #include<cstdlib> #include<cmath> #include<cassert> #include<sstream> #include<iostream> #include<string> #include<vector> #include<queue> #include<set> #include<map> #include<utility> #include<numeric> #include<algorithm> #include<bitset> #include<complex> #include<cstring> using namespace std; typedef unsigned uint; typedef long long Int; typedef vector<int> vint; typedef pair<int,int> pint; typedef pair<double,double> pt; int INF=1001001001; double dis(pt p1,pt p2){ return (p1.first-p2.first)*(p1.first-p2.first)+(p1.second-p2.second)*(p1.second-p2.second); } pt linedist00(double x,double y,double vx,double vy){ double l=(x*vy-y*vx)/(vx*vx+vy*vy); return pt(l*vy,-l*vx); } pair<double,pair<double,double> > culc(double r,double x,double y,double vx,double vy){ pt p2=linedist00(x,y,vx,vy); double vv=dis(pt(0,0),pt(vx,vy)); double d2=dis(pt(0,0),p2); if(d2 <= r*r){ double l=r*r-d2; double t; if(vx==0 && vy==0) return pair<double,pair<double,double> >(INF,pair<double,double>(INF,INF)); else if(vx==0) t= (p2.second-y)/vy; else t= (p2.first-x)/vx; double dt=sqrt(l/vv); return pair<double,pair<double,double> >(t-dt,pair<double,double>(t,t+dt)); }else{ return pair<double,pair<double,double> >(INF,pair<double,double>(INF,INF)); } } struct str{ double t; pint p; int f; str(double t0,pint p0,int f0){ t=t0; p=p0; f=f0; } }; void prints(str a){ printf("t=%f %d---%d %d\n",a.t,a.p.first,a.p.second,a.f); } bool operator<(str a,str b){ return a.t>b.t; } int main(){ int i,j,k; int n,t,r; pt xy[100]; pair<int,int> v[100][1000]; int p[100]; string name[1000]; int chk[100]; while(1){ cin >> n >> t >> r; if(n==0) break; for(i=0;i<100;i++){ chk[i]=0; for(j=0;j<1000;j++){ v[i][j]=pint(INF,INF); } } for(i=0;i<n;i++){ cin >> name[i]; cin >> k >> xy[i].first >> xy[i].second; while(1){ int tk,vx,vy; scanf("%d %d %d",&tk,&vx,&vy); v[i][tk-1].first=vx; v[i][tk-1].second=vy; if(tk==t) break; } } for(k=0;k<n;k++){ pint tmp=v[k][t-1]; for(i=t-2;i>=0;i--){ if(v[k][i].first==INF) v[k][i]=tmp; else tmp=v[k][i]; } } int a[100][100]; for(j=0;j<n;j++){ for(k=0;k<n;k++){ if(dis(xy[j],xy[k]) <= r*r) a[j][k]=1; else a[j][k]=0; } } queue<int> q; q.push(0); while(!q.empty()){ int f=q.front(); q.pop(); if(chk[f]==1) continue; else{ chk[f]=1; for(i=0;i<n;i++){ if(a[f][i]==1) q.push(i); } } } for(i=0;i<t;i++){ priority_queue<str> pq; for(j=0;j<n;j++){ for(k=j+1;k<n;k++){ int x1=xy[k].first-xy[j].first,y1=xy[k].second-xy[j].second; int vx=v[k][i].first-v[j][i].first,vy=v[k][i].second-v[j][i].second; pair<double,pair<double,double> > time=culc(r,x1,y1,vx,vy); if(time.first==INF) continue; double t1=INF,t2=INF; if(time.first >= 0.0){ if(time.second.second <= 1.0){ t1=time.first; t2=time.second.second; }else if(time.first <= 1.0){ t1=time.first; } }else{ if(time.second.second>=0 && time.second.second<=1.0){ t2=time.second.second; } } if(t1!=INF) pq.push(str(t1,pint(j,k),1)); if(t2!=INF) pq.push(str(t2,pint(j,k),0)); } } while(!pq.empty()){ str tmp=pq.top(); pq.pop(); // prints(tmp); a[tmp.p.first][tmp.p.second]=tmp.f; a[tmp.p.second][tmp.p.first]=tmp.f; if(tmp.f==1){ if(chk[tmp.p.first]==0 && chk[tmp.p.second]==1) swap(tmp.p.first,tmp.p.second); if(chk[tmp.p.first]==1 && chk[tmp.p.second]==0){ queue<int> q2; q2.push(tmp.p.second); chk[tmp.p.second]=1; while(!q2.empty()){ k=q2.front(); q2.pop(); for(j=0;j<n;j++){ if(a[j][k]==1 && chk[j]==0){ chk[j]=1; q2.push(j); } } } } } } for(j=0;j<n;j++){ xy[j].first+=v[j][i].first; xy[j].second+=v[j][i].second; } } vector<string> ans; int cnt=0; for(i=0;i<n;i++){ if(chk[i]==1){ ans.push_back(name[i]); cnt++; } } sort(ans.begin(),ans.end()); for(i=0;i<cnt;i++){ cout << ans[i] << endl; } } return 0; }
#include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <climits> #include <cfloat> using namespace std; int h, w, n; vector<string> grid; class Data { public: vector<int> p; bitset<3> bs; Data(vector<int>& p0, bitset<3> bs0){ p = p0; bs = bs0; } int toInt(){ int ret = bs.to_ulong(); for(int i=0; i<n; ++i){ ret *= h * w; ret += p[i]; } return ret; } }; int solve() { string gridLine = accumulate(grid.begin(), grid.end(), string()); int diff[] = {1, -1, w, -w}; vector<int> sp(n), gp(n); for(int i=0; i<h; ++i){ for(int j=0; j<w; ++j){ int c = grid[i][j]; if('a' <= c && c <= 'c'){ sp[c-'a'] = i * w + j; }else if('A' <= c && c <= 'C'){ gp[c-'A'] = i * w + j; } } } int size = 1 << n; for(int i=0; i<n; ++i) size *= h * w; vector<vector<bool> > check(2, vector<bool>(size, false)); check[0][Data(sp, 0).toInt()] = true; check[1][Data(gp, 0).toInt()] = true; vector<deque<Data> > dq(2); dq[0].push_back(Data(sp, 0)); dq[1].push_back(Data(gp, 0)); int turn = 0; int m = 1; int ret = 1; for(;;){ if(m == 0){ ++ ret; turn ^= 1; m = dq[turn].size(); } Data d = dq[turn].front(); dq[turn].pop_front(); -- m; for(int i=0; i<n; ++i){ if(d.bs[i]) continue; d.bs[i] = true; for(int j=0; j<4; ++j){ d.p[i] += diff[j]; bool ok = true; if(gridLine[d.p[i]] == '#') ok = false; for(int k=0; k<n; ++k){ if(k != i && d.p[k] == d.p[i]) ok = false; } if(ok){ int a = d.toInt(); if(!check[turn][a]){ dq[turn].push_front(d); ++ m; check[turn][a] = true; } } d.p[i] -= diff[j]; } d.bs[i] = false; } d.bs = 0; int a = d.toInt(); if(!check[turn][a]){ if(check[turn^1][a]) return ret; dq[turn].push_back(d); check[turn][a] = true; } } } int main() { for(;;){ cin >> w >> h >> n; if(w == 0) return 0; cin.ignore(); grid.resize(h); for(int i=0; i<h; ++i) getline(cin, grid[i]); cout << solve() << endl; } }
#include <iostream> #include <queue> #include <string> #include <cstring> using namespace std; int w, h, n; string b[16]; int sx[3], sy[3], gx[3], gy[3]; short v[1 << 24]; short dist[3][16][16]; int dx[5] = { 0, 1, 0, -1, 0 }, dy[5] = { 0, 0, 1, 0, -1 }; struct S { unsigned char x[3], y[3]; float h; bool operator < (const S& s) const { return h > s.h; } }; float calcH(S& s) { float ret = 0; for(int i = 0; i < n; i++) { ret = max(ret, (float)dist[i][s.y[i]][s.x[i]]); } return 1.1 * ret; } int conv(S s) { int ret = 0; for(int i = 0; i < n; i++) { ret = ret * 16 + s.x[i]; } for(int i = 0; i < n; i++) { ret = ret * 16 + s.y[i]; } return ret; } bool check(S s) { for(int i = 0; i < n; i++) { if(s.x[i] != gx[i] || s.y[i] != gy[i]) return false; } return true; } bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; } bool valid(S s, S ps) { for(int i = 0; i < n; i++) { if(!in(s.x[i], s.y[i])) return false; if(b[s.y[i]][s.x[i]] == '#') return false; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(s.x[i] == s.x[j] && s.y[i] == s.y[j]) return false; if(ps.x[j] == s.x[i] && ps.y[j] == s.y[i] && ps.x[i] == s.x[j] && ps.y[i] == s.y[j]) return false; } } return true; } void view(S s) { string temp[16]; for(int i = 0; i < h; i++) { temp[i] = b[i]; } for(int i = 0; i < n; i++) { temp[s.y[i]][s.x[i]] = 'a' + i; } for(int i = 0; i < h; i++) { cout << temp[i] << endl; } cout << endl; } void makeNS(int i, S s, S& ps, vector<S>& res) { if(i == n) { if(valid(s, ps)) res.push_back(s); return; } for(int k = 0; k < 5; k++) { S ns = s; ns.x[i] += dx[k]; ns.y[i] += dy[k]; makeNS(i + 1, ns, ps, res); } } int main() { while(cin >> w >> h >> n, w | h | n) { cin.ignore(); S s; for(int i = 0; i < h; i++) { getline(cin, b[i]); for(int j = 0; j < w; j++) { if(b[i][j] == 'a') s.x[0] = j, s.y[0] = i; if(b[i][j] == 'b') s.x[1] = j, s.y[1] = i; if(b[i][j] == 'c') s.x[2] = j, s.y[2] = i; if(b[i][j] == 'A') gx[0] = j, gy[0] = i; if(b[i][j] == 'B') gx[1] = j, gy[1] = i; if(b[i][j] == 'C') gx[2] = j, gy[2] = i; if(b[i][j] == 'a' || b[i][j] == 'b' || b[i][j] == 'c') b[i][j] = ' '; } //cout << b[i] << endl; } memset(dist, -1, sizeof dist); for(int l = 0; l < n; l++) { for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(b[i][j] == '#') continue; queue<pair<int, int>> q; q.push(make_pair(gx[l], gy[l])); dist[l][gy[l]][gx[l]] = 0; while(q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for(int k = 1; k < 5; k++) { int nx = x + dx[k], ny = y + dy[k]; if(b[ny][nx] == '#') continue; if(dist[l][ny][nx] != -1) continue; dist[l][ny][nx] = dist[l][y][x] + 1; q.push(make_pair(nx, ny)); } } } } } memset(v, -1, sizeof v); priority_queue<S> q; s.h = calcH(s); q.push(s); v[conv(s)] = 0; int ans = -1; while(q.size()) { s = q.top(); q.pop(); int d = v[conv(s)]; //cout << conv(s) << " " << d << endl; //view(s); if(check(s)) { ans = d; break; } vector<S> NS; makeNS(0, s, s, NS); for(S ns : NS) { int X = conv(ns); if(v[X] != -1 && v[X] <= d + 1) continue; v[X] = d + 1; ns.h = calcH(ns) + d + 1; q.push(ns); } } cout << ans << endl; //return 0; } }
#include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <set> #include <cstring> #include <queue> #include <map> #include <list> #include <cmath> #include <iomanip> #define INF 0x3f3f3f3f #define EPS 10e-5 typedef long long ll; const int dx[]={-1,0,0,1,0},dy[]={0,1,-1,0,0}; using namespace std; int w,h,n,ter; char mps[20][20]; pair<char,char> pos[3]; pair<char,char> tar[3]; bool dp[1<<25]; int ncs[1<<10][5]; int nle[1<<10]; const int queS = 1<<22; pair<int,int> aps[queS]; int ans,en; int ecnt = 0,bcnt=0; void push(const pair<int,int>&a) { aps[ecnt++] = a; ecnt%=queS; } int solve(){ int hash = 0,ans=0; bcnt = ecnt = 0; int sa = 0; for(int i=0;i<n;++i){ hash += pos[i].first<<(i*8+4); hash += pos[i].second<<(i*8); ans += tar[i].first<<(i*8+4); ans += tar[i].second<<(i*8); } push(make_pair(hash,0)); while(bcnt!=ecnt){ pair<int,int> now = aps[bcnt++]; bcnt%=queS; if(now.first==ans) { sa = now.second;break; } if(dp[now.first]) continue; dp[now.first] = true; if(n==1){ int u = now.first&0xff; for(int i = 0;i<nle[u];++i){ if(dp[ncs[u][i]]) continue; push(make_pair(ncs[u][i],now.second+1)); } }else if(n==2){ int u = now.first&0xff,v = (now.first>>8)&0xff; for(int i=0;i<nle[u];++i) for(int j=0;j<nle[v];++j){ if(dp[ncs[u][i]+(ncs[v][j]<<8)]||ncs[u][i]==ncs[v][j] ||(ncs[u][i] == v&& ncs[v][j] == u)) continue; push(make_pair(ncs[u][i]|(ncs[v][j]<<8),now.second+1)); } }else{ int u = now.first&0xff,v = (now.first>>8)&0xff,a = (now.first>>16)&0xff; for(int i=0;i<nle[u];++i) for(int j=0;j<nle[v];++j) for(int k=0;k<nle[a];++k){ if(dp[ncs[u][i]+(ncs[v][j]<<8)+(ncs[a][k]<<16)]|| ncs[u][i]==ncs[v][j]||ncs[u][i]==ncs[a][k] ||ncs[v][j]==ncs[a][k] ||(ncs[v][j]==u&&v==ncs[u][i]) ||(a==ncs[u][i]&&u==ncs[a][k]) ||(ncs[v][j]==a&&v==ncs[a][k])) continue; push(make_pair(ncs[u][i]|(ncs[v][j]<<8)|(ncs[a][k]<<16),now.second+1)); } } } return sa; } int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); while(~scanf("%d%d%d\n",&w,&h,&n)){ if(w==0&&h==0&&n==0) break; memset(dp,0,sizeof(dp)); memset(pos,0,sizeof(pos)); memset(tar,0,sizeof(tar)); memset(nle,0,sizeof(nle)); for(int i=0;i<h;++i) { for(int j=0;j<w;++j) { scanf("%c",mps[i]+j); if(mps[i][j]!='#'&&mps[i][j]!=' '){ if(islower(mps[i][j])){ pos[mps[i][j]-'a'].first = i; pos[mps[i][j]-'a'].second = j; }else{ tar[mps[i][j]-'A'].first = i; tar[mps[i][j]-'A'].second = j; mps[i][j] = ' '; } } } getchar(); } for(int i=1;i<h;++i){ for(int j=1;j<w;++j){ if(mps[i][j]!='#'){ for(int k=0;k<4;++k){ if(mps[i+dy[k]][j+dx[k]]!='#'){ ncs[(i<<4)+j][nle[(i<<4)+j]++] = (((i+dy[k])<<4)+dx[k]+j); } } ncs[(i<<4)+j][nle[(i<<4)+j]++] = (i<<4)+j; } } } printf("%d\n",solve()); } return 0; }
#include<iostream> #include<cstdio> #include<queue> #include<map> #include<set> #include<deque> #include<algorithm> #include<cassert> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define inf (1<<29) #define MAX 17 using namespace std; typedef pair<int,int> ii; int h,w,n; char G[MAX][MAX]; //unsigned char ev[MAX*MAX][MAX*MAX]; //unsigned char mincost[MAX*MAX][MAX*MAX][MAX*MAX]; short ev[MAX*MAX][MAX*MAX]; short mincost[MAX*MAX][MAX*MAX][MAX*MAX]; int dx[] = {0,1,0,-1,0}; int dy[] = {1,0,-1,0,0}; int goal[3],sp[3]; char cc[3]; struct Pox { int cur[3]; //unsigned char cost; short cost; Pox(int a=255,int b=255,int c=255,/*unsigned char*/short cost=1000/*255*/):cost(cost) { cur[0] = a, cur[1] = b, cur[2] = c; } bool operator < (const Pox &a)const { return cost > a.cost; } }; void print(char a[MAX][MAX]) { rep(i,h) { rep(j,w)cout << a[i][j]; cout << endl; } cout << endl; } bool isValidMove(int p11,int p12,int p21,int p22) { if(p12 == p22)return false;//visited same point if(p12 == p21 && p11 == p22)return false;//cross return true; } void initEv() { rep(y,h*w)rep(x,h*w)ev[y][x] = 1000;//255; rep(y,h)rep(x,w) { deque<ii> deq; deq.push_back(ii(x+y*w,0)); ev[x+y*w][x+y*w] = 0; while(!deq.empty()) { ii state = deq.front(); deq.pop_front(); int cx = state.first % w; int cy = state.first / w; rep(i,4) { int nx = cx + dx[i]; int ny = cy + dy[i]; if(!(0 <= nx && nx < w && 0 <= ny && ny < h))continue; if(G[ny][nx] == '#')continue; if(ev[x+y*w][nx+ny*w] > state.second + 1) { ev[x+y*w][nx+ny*w] = state.second + 1; deq.push_back(ii(nx+ny*w,state.second+1)); } } } } } bool fin_check(int *cur) { rep(i,n)if(cur[i] != goal[i])return false; return true; } void compute() { /* rep(i,n) { cout << "goal[" << i << "] = " << goal[i] % w << "," << goal[i] / w << endl; } */ rep(i,MAX*MAX)rep(j,MAX*MAX)rep(k,MAX*MAX)mincost[i][j][k] = 1000;//(unsigned char)255; priority_queue<Pox> Q; mincost[sp[0]][sp[1]][sp[2]] = 1000;//(unsigned char)0; Q.push(Pox(sp[0],sp[1],sp[2],0)); while(!Q.empty()) { Pox pox = Q.top(); Q.pop(); // cout << "pox ( ( " << pox.cur[0] << " " << pox.cur[0] % w << "," << pox.cur[0] / w << ") ( " << pox.cur[1] % w << "," << pox.cur[1] / w << " ) ) : " << (int)pox.cost << " \n"; if(fin_check(pox.cur)) { cout << (int)pox.cost << endl; return; } if(n == 1) { int x = pox.cur[0] % w; int y = pox.cur[0] / w; rep(i,5) { int nx = x + dx[i]; int ny = y + dy[i]; if(!( 0 <= nx && nx < w && 0 <= ny && ny < h ))continue; if(G[ny][nx] == '#')continue; //cout << "mincost[" << pox.cur[0] << "][0][0] = " << (int)mincost[pox.cur[0]][0][0] << " > " << (pox.cost + 1) << endl; if(mincost[nx+ny*w][0][0] > /*(unsigned char)*/(pox.cost + 1)) { mincost[nx+ny*w][0][0] = pox.cost + 1; Pox next = Pox(nx+ny*w,0,0,pox.cost+1); Q.push(next); } } } else if(n == 2) { int x1 = pox.cur[0] % w; int y1 = pox.cur[0] / w; int x2 = pox.cur[1] % w; int y2 = pox.cur[1] / w; rep(i,5) { int nx1 = x1 + dx[i]; int ny1 = y1 + dy[i]; if(!( 0 <= nx1 && nx1 < w && 0 <= ny1 && ny1 < h ))continue; if(G[ny1][nx1] == '#')continue; rep(j,5) { int nx2 = x2 + dx[j]; int ny2 = y2 + dy[j]; if(!( 0 <= nx2 && nx2 < w && 0 <= ny2 && ny2 < h ))continue; if(G[ny2][nx2] == '#')continue; if(!isValidMove(pox.cur[0],nx1+ny1*w,pox.cur[1],nx2+ny2*w))continue; //cout << "here : " << (int)mincost[nx1+ny1*w][nx2+ny2*w][0] << " > " << pox.cost + 1 << endl; if(mincost[nx1+ny1*w][nx2+ny2*w][0] > /*(unsigned char)*/(pox.cost + 1)) { mincost[nx1+ny1*w][nx2+ny2*w][0] = pox.cost + 1; Q.push(Pox(nx1+ny1*w,nx2+ny2*w,0,pox.cost+1)); } } } } else if(n == 3) { int x[3],y[3],nx[3],ny[3]; rep(i,3)x[i] = pox.cur[i] % w, y[i] = pox.cur[i] / w; rep(i,5) { nx[0] = x[0] + dx[i]; ny[0] = y[0] + dy[i]; if(!( 0 <= nx[0] && nx[0] < w && 0 <= ny[0] && ny[0] < h ))continue; if(G[ny[0]][nx[0]] == '#')continue; rep(j,5) { nx[1] = x[1] + dx[j]; ny[1] = y[1] + dy[j]; if(!( 0 <= nx[1] && nx[1] < w && 0 <= ny[1] && ny[1] < h ))continue; if(G[ny[1]][nx[1]] == '#')continue; if(!isValidMove(pox.cur[0],nx[0]+ny[0]*w,pox.cur[1],nx[1]+ny[1]*w))continue; rep(k,5) { nx[2] = x[2] + dx[k]; ny[2] = y[2] + dy[k]; if(!( 0 <= nx[2] && nx[2] < w && 0 <= ny[2] && ny[2] < h ))continue; if(G[ny[2]][nx[2]] == '#')continue; if(!isValidMove(pox.cur[0],nx[0]+ny[0]*w,pox.cur[2],nx[2]+ny[2]*w))continue; if(!isValidMove(pox.cur[1],nx[1]+ny[1]*w,pox.cur[2],nx[2]+ny[2]*w))continue; if(mincost[nx[0]+ny[0]*w][nx[1]+ny[1]*w][nx[2]+ny[2]*w] > /*(unsigned char)*/(pox.cost + 1)) { mincost[nx[0]+ny[0]*w][nx[1]+ny[1]*w][nx[2]+ny[2]*w] = pox.cost + 1; Q.push(Pox(nx[0]+ny[0]*w,nx[1]+ny[1]*w,nx[2]+ny[2]*w,pox.cost+1)); } } } } } } assert(false); } int main() { while(scanf("%d %d %d",&w,&h,&n),h|w|n) { rep(i,3)sp[i] = 0; int cd = 0; map<char,int> dex; rep(i,h) { getchar(); rep(j,w) { scanf("%c",&G[i][j]); dex[G[i][j]] = j + i * w; if('a' <= G[i][j] && G[i][j] <= 'z') { sp[cd] = j + i * w; cc[cd++] = G[i][j]; } } } assert(n == cd); rep(i,n)goal[i] = dex['A'+cc[i]-'a']; //print(G); initEv(); compute(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long double EPS = 1e-10; const long long INF = 1e18; const long double PI = acos(-1.0L); //const ll mod = 1000000007; struct node { int ah, aw, bh, bw, ch, cw; node(int _ah, int _aw, int _bh, int _bw, int _ch, int _cw) { ah = _ah; aw = _aw; bh = _bh; bw = _bw; ch = _ch; cw = _cw; } node() { ah = aw = bh = bw = ch = cw = 0; } }; int H, W, N; int dp[14][14][14][14][14][14]; int dh[5] = {0, 1, -1, 0, 0}; int dw[5] = {0, 0, 0, 1, -1}; string field[14]; string dust; void solve() { H -= 2; W -= 2; //cerr << H << " " << W << " " << N << endl; getline(cin, dust); getline(cin, dust); for(int h = 0; h < H; h++) { getline(cin, field[h]); string tmp = field[h].substr(1, W); field[h] = tmp; //field[h] = field[h].substr(1, W); //cerr << h << " " << field[h] << endl; } getline(cin, dust); node s; for(int h = 0; h < H; h++) { for(int w = 0; w < W; w++) { if(field[h][w] == 'a') { s.ah = h; s.aw = w; } if(field[h][w] == 'b') { s.bh = h; s.bw = w; } if(field[h][w] == 'c') { s.ch = h; s.cw = w; } } } queue<node> que; que.push(s); for(int h = 0; h < H; h++) { for(int w = 0; w < W; w++) { for(int k = 0; k < H; k++) { for(int l = 0; l < W; l++) { for(int a = 0; a < H; a++) { for(int b = 0; b < W; b++) { dp[h][w][k][l][a][b] = 1e9; } } } } } } dp[s.ah][s.aw][s.bh][s.bw][s.ch][s.cw] = 0; while(!que.empty()) { node now = que.front(); int cost = dp[now.ah][now.aw][now.bh][now.bw][now.ch][now.cw]; //cerr << now.ah << " " << now.aw << " " << now.bh << " " << now.bw << " " << now.ch << " " << now.cw << " " << cost << endl; if(field[now.ah][now.aw] == 'A') { if(N <= 1 or field[now.bh][now.bw] == 'B') { if(N <= 2 or field[now.ch][now.cw] == 'C') { cout << cost << endl; break; } } } que.pop(); for(int ka = 0; ka < 5; ka++) { int newah = now.ah + dh[ka]; int newaw = now.aw + dw[ka]; if(newah < 0 or newah >= H) continue; if(newaw < 0 or newaw >= W) continue; if(field[newah][newaw] == '#') continue; for(int kb = 0; kb < 5; kb++) { if(N <= 1 and kb >= 1) break; int newbh = now.bh + dh[kb]; int newbw = now.bw + dw[kb]; if(newbh < 0 or newbh >= H) continue; if(newbw < 0 or newbw >= W) continue; if(N >= 2) { if(newah == newbh and newaw == newbw) continue; if(newah == now.bh and newaw == now.bw and newbh == now.ah and newbw == now.aw) continue; if(field[newbh][newbw] == '#') continue; } for(int kc = 0; kc < 5; kc++) { if(N <= 2 and kc >= 1) break; int newch = now.ch + dh[kc]; int newcw = now.cw + dw[kc]; if(newch < 0 or newch >= H) continue; if(newcw < 0 or newcw >= W) continue; if(N >= 3) { if(newah == newch and newaw == newcw) continue; if(newah == now.ch and newaw == now.cw and newch == now.ah and newcw == now.aw) continue; if(newbh == newch and newbw == newcw) continue; if(newbh == now.ch and newbw == now.cw and newch == now.bh and newcw == now.bw) continue; if(field[newch][newcw] == '#') continue; } if(chmin(dp[newah][newaw][newbh][newbw][newch][newcw], dp[now.ah][now.aw][now.bh][now.bw][now.ch][now.cw] + 1)) { node to(newah, newaw, newbh, newbw, newch, newcw); que.push(to); } } } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); while(cin >> W >> H >> N) { if(H == 0) break; solve(); } return 0; }
#pragma GCC optimize "O3" #pragma GCC target "tune=native" #pragma GCC target "avx" #include <bits/stdc++.h> using namespace std; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define all(v) v.begin(),v.end() const int INF = 1 << 30; const int dx[] = {0, 1, 0, -1, 0}; const int dy[] = {1, 0, -1, 0, 0}; struct State { array<int, 3> xs, ys; State() { fill(all(xs), 0); fill(all(ys), 0); } int hash() { int a = 0; rep(i, 3) a = a | (xs[i] << (4 * i)); rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3)); return a; } }; array<short, 1 << 24> dist; bool solve() { int W, H, N; vector<string> G; cin >> W >> H >> N; if(W == 0) return false; cin.ignore(); rep(i, H) { string s; getline(cin, s); G.push_back(s); } State init, last; rep(y, H) rep(x, W) { int i = G[y][x] - 'a'; int j = G[y][x] - 'A'; if(0 <= i and i < N) { init.ys[i] = y; init.xs[i] = x; } if(0 <= j and j < N) { last.ys[j] = y; last.xs[j] = x; } } auto on_field = [&] (int y, int x) { return 0 <= y and y < H and 0 <= x and x < W and G[y][x] !='#'; }; auto move = [&] (State &s, int idx, int dir) { s.xs[idx] += dx[dir]; s.ys[idx] += dy[dir]; return on_field(s.ys[idx], s.xs[idx]); }; auto same = [&] (State &s, int i) { rep(j, i) { if(s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j]) return true; } return false; }; auto swapped = [&] (State &n, State &p) { rep(i, N) rep(j, N) if(i != j) { if((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and (n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j])) return true; } return false; }; int last_hash = last.hash(); queue<State> que; fill(all(dist), -1); que.push(init); dist[init.hash()] = 0; while(que.size()) { State now = que.front(); que.pop(); int nh = now.hash(); rep(i, 5) { State nxt1 = now; if(not move(nxt1, 0, i)) continue; rep(j, 5) { State nxt2 = nxt1; if(N >= 2 and not move(nxt2, 1, j)) continue; if(N >= 2 and same(nxt2, 1)) continue; rep(k, 5) { State nxt3 = nxt2; if(N >= 3 and not move(nxt3, 2, k)) continue; if(N >= 3 and same(nxt3, 2)) continue; if(not swapped(nxt3, now)) { int h = nxt3.hash(); if(last_hash == h) { cout << dist[nh] + 1 << endl; return 1; } if(dist[h] < 0) { dist[h] = dist[nh] + 1; que.push(nxt3); } } } } } } assert(0); return 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); while(solve()); return 0; }
#include <iostream> #include <queue> #include <string> #include <cstring> using namespace std; int w, h, n; string b[16]; int sx[3], sy[3], gx[3], gy[3]; short v[1 << 24]; short dist[3][16][16]; int dx[5] = { 0, 1, 0, -1, 0 }, dy[5] = { 0, 0, 1, 0, -1 }; struct S { unsigned char x[3], y[3]; float h; bool operator < (const S& s) const { return h > s.h; } }; float calcH(S& s) { float ret = 0; for(int i = 0; i < n; i++) { ret = max(ret, (float)dist[i][s.y[i]][s.x[i]]); } return ret; } int conv(S s) { int ret = 0; for(int i = 0; i < n; i++) { ret = ret * 16 + s.x[i]; } for(int i = 0; i < n; i++) { ret = ret * 16 + s.y[i]; } return ret; } bool check(S s) { for(int i = 0; i < n; i++) { if(s.x[i] != gx[i] || s.y[i] != gy[i]) return false; } return true; } bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; } bool valid(S s, S ps) { for(int i = 0; i < n; i++) { if(!in(s.x[i], s.y[i])) return false; if(b[s.y[i]][s.x[i]] == '#') return false; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(s.x[i] == s.x[j] && s.y[i] == s.y[j]) return false; if(ps.x[j] == s.x[i] && ps.y[j] == s.y[i] && ps.x[i] == s.x[j] && ps.y[i] == s.y[j]) return false; } } return true; } void view(S s) { string temp[16]; for(int i = 0; i < h; i++) { temp[i] = b[i]; } for(int i = 0; i < n; i++) { temp[s.y[i]][s.x[i]] = 'a' + i; } for(int i = 0; i < h; i++) { cout << temp[i] << endl; } cout << endl; } void makeNS(int i, S s, S& ps, vector<S>& res) { if(i == n) { if(valid(s, ps)) res.push_back(s); return; } for(int k = 0; k < 5; k++) { S ns = s; ns.x[i] += dx[k]; ns.y[i] += dy[k]; makeNS(i + 1, ns, ps, res); } } int main() { while(cin >> w >> h >> n, w | h | n) { cin.ignore(); S s; for(int i = 0; i < h; i++) { getline(cin, b[i]); for(int j = 0; j < w; j++) { if(b[i][j] == 'a') s.x[0] = j, s.y[0] = i; if(b[i][j] == 'b') s.x[1] = j, s.y[1] = i; if(b[i][j] == 'c') s.x[2] = j, s.y[2] = i; if(b[i][j] == 'A') gx[0] = j, gy[0] = i; if(b[i][j] == 'B') gx[1] = j, gy[1] = i; if(b[i][j] == 'C') gx[2] = j, gy[2] = i; if(b[i][j] == 'a' || b[i][j] == 'b' || b[i][j] == 'c') b[i][j] = ' '; } //cout << b[i] << endl; } memset(dist, -1, sizeof dist); for(int l = 0; l < n; l++) { for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(b[i][j] == '#') continue; queue<pair<int, int>> q; q.push(make_pair(gx[l], gy[l])); dist[l][gy[l]][gx[l]] = 0; while(q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for(int k = 1; k < 5; k++) { int nx = x + dx[k], ny = y + dy[k]; if(b[ny][nx] == '#') continue; if(dist[l][ny][nx] != -1) continue; dist[l][ny][nx] = dist[l][y][x] + 1; q.push(make_pair(nx, ny)); } } } } } memset(v, -1, sizeof v); priority_queue<S> q; s.h = calcH(s); q.push(s); v[conv(s)] = 0; int ans = -1; while(q.size()) { s = q.top(); q.pop(); int d = v[conv(s)]; //cout << conv(s) << " " << d << endl; //view(s); if(check(s)) { ans = d; break; } vector<S> NS; makeNS(0, s, s, NS); for(S ns : NS) { int X = conv(ns); if(v[X] != -1 && v[X] <= d + 1) continue; v[X] = d + 1; ns.h = calcH(ns) + d + 1; q.push(ns); } } cout << ans << endl; } }
/* * D.cpp * * Created on: Oct 11, 2013 * Author: Hesham */ #include<iostream> #include<string> #include<map> #include<ctime> #include<vector> #include<queue> #include<stack> #include<set> #include<algorithm> #include<sstream> #include<iomanip> #include<cstring> #include<bitset> #include<fstream> #include<cmath> #include<cassert> #include <stdio.h> #include<ctype.h> using namespace std; int dx[] = { 0, 0, 1, -1, 0 }; int dy[] = { 0, 1, 0, 0, -1 }; int w, h, n; string house[16]; struct State { vector<int> y, x; State() { x.resize(3, 0); y.resize(3, 0); } State(vector<int>& yy, vector<int>& xx) : x(xx), y(yy) { } bool operator ==(const State& other) const { return x == other.x && y == other.y; } }; bool vis1[16][16][16][16][16][16]; bool vis2[16][16][16][16][16][16]; //short dist[16][16][16][16][16][16]; bool isVis(State& s, bool vis[16][16][16][16][16][16]) { return vis[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]]; } void setVis(State& s, bool vis[16][16][16][16][16][16]) { vis[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]] = true; } /*int getDist(State& s) { return dist[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]]; }*/ /*void setDist(State& s, int v) { dist[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]] = v; }*/ bool ok(int y, int x) { if (y < 0 || y == h || x < 0 || x == w) return false; if (house[y][x] == '#') return false; return true; } int d1, d2; int c11, c12, c21, c22; int pushAdjs(int i, State& adj, State& curr, queue<State>& q, bool visA[16][16][16][16][16][16], bool visB[16][16][16][16][16][16], int da, int db, int& c) { if (i == n) { if (isVis(adj, visA)) return -1; if (isVis(adj, visB)) return da + 1 + db; setVis(adj, visA); q.push(adj); ++c; return -1; } for (int d = 0; d < 5; ++d) { adj.y[i] = dy[d] + curr.y[i]; adj.x[i] = dx[d] + curr.x[i]; if (!ok(adj.y[i], adj.x[i])) continue; bool okk = true; for (int j = 0; j < i; ++j) { if (adj.y[i] == curr.y[j] && adj.x[i] == curr.x[j]&& adj.y[j] == curr.y[i] && adj.x[j] == curr.x[i]) { okk = false; break; } if (adj.y[i] == adj.y[j] && adj.x[i] == adj.x[j]) { okk = false; break; } } if (!okk) continue; int cost = pushAdjs(i + 1, adj, curr, q, visA, visB, da, db, c); if (cost != -1) return cost; } return -1; } int meetMiddle(vector<int>& sy, vector<int>& sx, vector<int>& ty, vector<int>& tx) { memset(vis1, false, sizeof vis1); memset(vis2, false, sizeof vis2); State s(sy, sx); State t(ty, tx); //setDist(s, 0); //setDist(t, 0); d1 = 0; d2 = 0; c11 = 1, c12 = 0, c21 = 1, c22 = 0; setVis(s, vis1); setVis(t, vis2); queue<State> q1; queue<State> q2; q1.push(s); q2.push(t); while (true) { if(d1 <= d2) { State curr1 = q1.front(); q1.pop(); --c11; State adj; int cost = pushAdjs(0, adj, curr1, q1, vis1, vis2, d1, d2, c12); if (cost != -1) return cost; if(!c11){ swap(c11, c12); d1++; } } if(d2 < d1) { State curr2 = q2.front(); q2.pop(); --c21; State adj; int cost = pushAdjs(0, adj, curr2, q2, vis2, vis1, d2, d1, c22); if (cost != -1) return cost; if(!c21){ swap(c21, c22); d2++; } } } return -1; } int main() { //freopen("in.txt", "r", stdin); //clock_t begin = clock(); string line; while (cin >> w >> h >> n) { if (!w && !h && !n) break; cin.ignore(); cin.ignore(); for (int i = 0; i < h; ++i) getline(cin, house[i]); vector<int> sx(3, 0); vector<int> sy(3, 0); vector<int> tx(3, 0); vector<int> ty(3, 0); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (house[i][j] >= 'A' && house[i][j] <= 'C') { tx[(house[i][j] - 'A')] = j; ty[(house[i][j] - 'A')] = i; } if (house[i][j] >= 'a' && house[i][j] <= 'c') { sx[(house[i][j] - 'a')] = j; sy[(house[i][j] - 'a')] = i; } } } cout << meetMiddle(sy, sx, ty, tx) << endl; } }
#include <cstdio> #include <queue> #include <cstring> #include <string> #include <cctype> #include <iostream> using namespace std; char f[16][16]; int fx,fy,N; int dx[] = {0, 0, -1, 1, 0}; int dy[] = {0, -1, 0, 0, 1}; class Ghost { public: int x[3],y[3],c; bool r; Ghost(int* i, int* j, int c, bool r) :r(r),c(c) { for(int k=0; k<3; k++) { x[k]=i[k]; y[k]=j[k]; } } }; bool move(int x, int y, int p) { if(p>=N) return true; if(x<0||y<0||x>=fx||y>=fy) return false; if(f[x][y]=='#') return false; return true; } bool crash(int* x, int* y, int* gx, int* gy, int p) { if(p<N) return false; for(int i=0; i<p; i++) for(int j=i+1; j<p; j++) { if(x[i]==x[j]&&y[i]==y[j]) return true; if(x[i]==gx[j]&&y[i]==gy[j]&&gx[i]==x[j]&&gy[i]==y[j]) return true; } return false; } unsigned char h[256][256][256][2]; int main() { while(scanf("%d%d%d",&fx,&fy,&N), (fx||fy||N)) { int sx[3]={0},sy[3]={0},gx[3]={0},gy[3]={0}; memset(h,0,sizeof(h)); string s; getchar(); for(int i=0; i<fy; i++) { getline(cin, s); for(int j=0; j<fx; j++) { if(islower(s[j])) { sx[s[j]-'a']=j; sy[s[j]-'a']=i; } if(isupper(s[j])) { gx[s[j]-'A']=j; gy[s[j]-'A']=i; } f[j][i]=s[j]; } } queue<Ghost> q; q.push(Ghost(sx,sy,1,0)); q.push(Ghost(gx,gy,1,1)); int th[3]; for(int l=0; l<3; l++) { th[l]=(sx[l]<<4)+sy[l]; } h[th[0]][th[1]][th[2]][0]=1; for(int l=0; l<3; l++) { th[l]=(gx[l]<<4)+gy[l]; } h[th[0]][th[1]][th[2]][1]=1; while(!q.empty()) { Ghost g=q.front(); q.pop(); int th[3]; for(int l=0; l<3; l++) { th[l]=(g.x[l]<<4)+g.y[l]; } if(h[th[0]][th[1]][th[2]][!g.r]) { printf("%d\n", g.c+h[th[0]][th[1]][th[2]][!g.r]-1); break; } int tx[3],ty[3]; for(int i=0; i<3; i++) { tx[i]=g.x[i]; ty[i]=g.y[i]; } for(int i=0; i<5; i++) { tx[0]=g.x[0]+dx[i], ty[0]=g.y[0]+dy[i]; if(!move(tx[0],ty[0],0)) { tx[0]=g.x[0]; ty[0]=g.y[0]; continue; } for(int j=0; j<(N>1?5:1); j++) { tx[1]=g.x[1]+dx[j], ty[1]=g.y[1]+dy[j]; if(N>=2) { if(!move(tx[1],ty[1], 1) || (crash(tx,ty,g.x,g.y,2))) { tx[1]=g.x[1]; ty[1]=g.y[1]; continue; } } for(int k=0; k<(N>2?5:1); k++) { tx[2]=g.x[2]+dx[k], ty[2]=g.y[2]+dy[k]; if(N>=3) { if(!move(tx[2],ty[2], 2) || (crash(tx,ty,g.x,g.y,3))) { tx[2]=g.x[2]; ty[2]=g.y[2]; continue; } } for(int l=0; l<3; l++) { th[l]=(tx[l]<<4)+ty[l]; } if(h[th[0]][th[1]][th[2]][g.r]) { tx[2]=g.x[2]; ty[2]=g.y[2]; continue; } h[th[0]][th[1]][th[2]][g.r]=g.c; q.push(Ghost(tx, ty, g.c+1,g.r)); } } } } } }
#include <iostream> #include <queue> #include <string> #include <cstring> using namespace std; int w, h, n; string b[16]; int sx[3], sy[3], gx[3], gy[3]; short v[1 << 24]; short dist[3][16][16]; int dx[5] = { 0, 1, 0, -1, 0 }, dy[5] = { 0, 0, 1, 0, -1 }; struct S { int x[3], y[3]; float h; bool operator < (const S& s) const { return h > s.h; } }; float calcH(S& s) { float ret = 0; for(int i = 0; i < n; i++) { ret = max(ret, (float)dist[i][s.y[i]][s.x[i]]); } return ret; } int conv(S s) { int ret = 0; for(int i = 0; i < n; i++) { ret = ret * 16 + s.x[i]; } for(int i = 0; i < n; i++) { ret = ret * 16 + s.y[i]; } return ret; } bool check(S s) { for(int i = 0; i < n; i++) { if(s.x[i] != gx[i] || s.y[i] != gy[i]) return false; } return true; } bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; } bool valid(S s, S ps) { for(int i = 0; i < n; i++) { if(!in(s.x[i], s.y[i])) return false; if(b[s.y[i]][s.x[i]] == '#') return false; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(s.x[i] == s.x[j] && s.y[i] == s.y[j]) return false; if(ps.x[j] == s.x[i] && ps.y[j] == s.y[i] && ps.x[i] == s.x[j] && ps.y[i] == s.y[j]) return false; } } return true; } void makeNS(int i, S s, S& ps, vector<S>& res) { if(i == n) { if(valid(s, ps)) res.push_back(s); return; } for(int k = 0; k < 5; k++) { S ns = s; ns.x[i] += dx[k]; ns.y[i] += dy[k]; makeNS(i + 1, ns, ps, res); } } int main() { while(cin >> w >> h >> n, w | h | n) { cin.ignore(); S s; for(int i = 0; i < h; i++) { getline(cin, b[i]); for(int j = 0; j < w; j++) { if(b[i][j] == 'a') s.x[0] = j, s.y[0] = i; if(b[i][j] == 'b') s.x[1] = j, s.y[1] = i; if(b[i][j] == 'c') s.x[2] = j, s.y[2] = i; if(b[i][j] == 'A') gx[0] = j, gy[0] = i; if(b[i][j] == 'B') gx[1] = j, gy[1] = i; if(b[i][j] == 'C') gx[2] = j, gy[2] = i; if(b[i][j] == 'a' || b[i][j] == 'b' || b[i][j] == 'c') b[i][j] = ' '; } } memset(dist, -1, sizeof dist); for(int l = 0; l < n; l++) { for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(b[i][j] == '#') continue; queue<pair<int, int>> q; q.push(make_pair(gx[l], gy[l])); dist[l][gy[l]][gx[l]] = 0; while(q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for(int k = 1; k < 5; k++) { int nx = x + dx[k], ny = y + dy[k]; if(b[ny][nx] == '#') continue; if(dist[l][ny][nx] != -1) continue; dist[l][ny][nx] = dist[l][y][x] + 1; q.push(make_pair(nx, ny)); } } } } } memset(v, -1, sizeof v); priority_queue<S> q; s.h = calcH(s); q.push(s); v[conv(s)] = 0; int ans = -1; while(q.size()) { s = q.top(); q.pop(); int d = v[conv(s)]; if(check(s)) { ans = d; break; } vector<S> NS; makeNS(0, s, s, NS); for(S ns : NS) { int X = conv(ns); if(v[X] != -1 && v[X] <= d + 1) continue; v[X] = d + 1; ns.h = calcH(ns) + d + 1; q.push(ns); } } cout << ans << endl; } }
#include<cstdio> #include<algorithm> #include<queue> #include<math.h> #include<string.h> using namespace std; const int dx[] = { -1, 1, 0, 0 }; const int dy[] = { 0, 0, -1, 1 }; int w, h, n; int mymap[400][10]; int deg[400]; int ghost[4], target[4]; int *hash_table; int *dist; int pos_count; queue<int> q1, q2; int judge1(int org1, int org2, int aft1, int aft2) { if ((aft1 == aft2) || (org1 == aft2 && org2 == aft1)) return 0; return 1; } int judge2(int parent, int child, int flag) { if (!hash_table[child]) { if (flag == 1) q1.push(child); else q2.push(child); hash_table[child] = flag; dist[child] = dist[parent] + 1; } else if (hash_table[child] != flag) { int result = dist[parent] + 1 + dist[child]; printf("%d\n", result); return 1; } return 0; } int search(int parent, int p1, int p2, int p3, int flag) { for (int d1 = 0; d1 < deg[p1]; d1++) { int c1 = mymap[p1][d1]; if (n == 1 && judge2(parent, c1, flag)) return 1; else if (n > 1) { for (int d2 = 0; d2 < deg[p2]; d2++) { int c2 = mymap[p2][d2]; if (!judge1(p1, p2, c1, c2)) continue; if (n == 2 && judge2(parent, c1 + pos_count * c2, flag)) return 1; else if (n == 3) { for (int d3 = 0; d3 < deg[p3]; d3++) { int c3 = mymap[p3][d3]; if (!(judge1(p1, p3, c1, c3) && judge1(p2, p3, c2, c3))) continue; if (judge2(parent, c1 + pos_count * c2 + pos_count * pos_count * c3, flag)) return 1; } } } } } return 0; } int bfs() { int status1 = ghost[0] + ghost[1] * pos_count + ghost[2] * pos_count*pos_count; int status2 = target[0] + target[1] * pos_count + target[2] * pos_count*pos_count; q1 = queue<int>(); q2 = queue<int>(); q1.push(status1); q2.push(status2); hash_table[status1] = 1; hash_table[status2] = 2; dist[status1] = 0; dist[status2] = 0; int layer_count = 0; while (!q1.empty() || !q2.empty()) { while (!q1.empty() && (dist[status1] == layer_count)) { status1 = q1.front(); int status11 = status1 % pos_count; int status12 = status1 / pos_count % pos_count; int status13 = status1 / pos_count / pos_count % pos_count; q1.pop(); if (search(status1, status11, status12, status13, 1)) return 1; } while (!q2.empty() && (dist[status2] == layer_count)) { status2 = q2.front(); int status21 = status2 % pos_count; int status22 = status2 / pos_count % pos_count; int status23 = status2 / pos_count / pos_count % pos_count; q2.pop(); if (search(status2, status21, status22, status23, 2)) return 1; } layer_count++; } return -1; } void init() { for (int i = 0; i < 400; i++) deg[i] = 0; } int main() { while (scanf("%d%d%d", &w, &h, &n) == 3 && n) { char map[20][20]; int map2[20][20]; memset(ghost, 0, sizeof(ghost)); memset(target, 0, sizeof(target)); memset(map2, -1, sizeof(map2)); pos_count = 0; for (int i = 0; i <= h; i++) { fgets(map[i], 20, stdin); if (i) { for (int j = 0; j < w; j++) { if (map[i][j] != '#') { ///printf("%d\t", map[i][j]); mymap[pos_count][0] = pos_count; if (map[i][j] >= 97 && map[i][j] <= 99) { ghost[map[i][j] % 97] = pos_count; } else if (map[i][j] >= 65 && map[i][j] <= 67) target[map[i][j] % 65] = pos_count; map2[i][j] = pos_count; pos_count++; } } } } int pos_count2 = 0; for (int i = 1; i <= h; i++) { for (int j = 0; j < w; j++) { int degree = 1; if (map2[i][j] != -1) { for (int k = 0; k < 4; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (map2[nx][ny] != -1) { int space_index = map2[nx][ny]; mymap[pos_count2][degree++] = space_index; } } deg[pos_count2++] = degree; } } } int size = 1; for (int i = 0; i < n; i++) size *= pos_count; hash_table = new int[size]; dist = new int[size]; for (int i = 0; i < size; i++) { hash_table[i] = 0; dist[i] = -1; } bfs(); delete[] hash_table; delete[] dist; } }
#include <iostream> #include <cstdio> #include <queue> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define all(c) (c).begin(),(c).end() #define show(x) cout << #x << " " << x << endl struct State{int ax,ay,bx,by,cx,cy;}; string s[16]; int W,H,N,dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0},inf=1e4; int Ax,Ay,Bx,By,Cx,Cy,ax,ay,bx,by,cx,cy; short int d[16][16][16][16][16][16]; bool is(int x,int y){ return (0<=x&&x<H&&0<=y&&y<W&&s[x][y]!='#'); } void three(){ queue<State> que; rep(ai,H) rep(aj,W) rep(bi,H) rep(bj,W) rep(ci,H) rep(cj,W) d[ai][aj][bi][bj][ci][cj]=inf; d[Ax][Ay][Bx][By][Cx][Cy]=0; que.push(State{Ax,Ay,Bx,By,Cx,Cy}); while(!que.empty()){ State st=que.front(); que.pop(); rep(ad,5){ int nax=st.ax+dx[ad],nay=st.ay+dy[ad]; if(!is(nax,nay)) continue; rep(bd,5){ int nbx=st.bx+dx[bd],nby=st.by+dy[bd]; if(!is(nbx,nby)) continue; rep(cd,5){ int ncx=st.cx+dx[cd],ncy=st.cy+dy[cd]; if(!is(ncx,ncy)) continue; if(d[nax][nay][nbx][nby][ncx][ncy]!=inf) continue; if(nax==nbx&&nay==nby) continue; if(nbx==ncx&&nby==ncy) continue; if(ncx==nax&&ncy==nay) continue; if(nax==st.bx&&nay==st.by&&nbx==st.ax&&nby==st.ay) continue; if(nbx==st.cx&&nby==st.cy&&ncx==st.bx&&ncy==st.by) continue; if(ncx==st.ax&&ncy==st.ay&&nax==st.cx&&nay==st.cy) continue; d[nax][nay][nbx][nby][ncx][ncy]=d[st.ax][st.ay][st.bx][st.by][st.cx][st.cy]+1; que.push(State{nax,nay,nbx,nby,ncx,ncy}); } } } } } void two(){ queue<State> que; rep(ai,H) rep(aj,W) rep(bi,H) rep(bj,W) d[ai][aj][bi][bj][0][0]=inf; d[Ax][Ay][Bx][By][0][0]=0; que.push(State{Ax,Ay,Bx,By,0,0}); while(!que.empty()){ State st=que.front(); que.pop(); rep(ad,5){ int nax=st.ax+dx[ad],nay=st.ay+dy[ad]; if(!is(nax,nay)) continue; rep(bd,5){ int nbx=st.bx+dx[bd],nby=st.by+dy[bd]; if(!is(nbx,nby)) continue; if(d[nax][nay][nbx][nby][0][0]!=inf) continue; if(nax==nbx&&nay==nby) continue; if(nax==st.bx&&nay==st.by&&nbx==st.ax&&nby==st.ay) continue; d[nax][nay][nbx][nby][0][0]=d[st.ax][st.ay][st.bx][st.by][0][0]+1; que.push(State{nax,nay,nbx,nby,0,0}); } } } } void one(){ queue<State> que; rep(ai,H) rep(aj,W) d[ai][aj][0][0][0][0]=inf; d[Ax][Ay][0][0][0][0]=0; que.push(State{Ax,Ay,0,0,0,0}); while(!que.empty()){ State st=que.front(); que.pop(); rep(ad,5){ int nax=st.ax+dx[ad],nay=st.ay+dy[ad]; if(!is(nax,nay)) continue; if(d[nax][nay][0][0][0][0]!=inf) continue; d[nax][nay][0][0][0][0]=d[st.ax][st.ay][0][0][0][0]+1; que.push(State{nax,nay,0,0,0,0}); } } } int main(){ while(true){ scanf("%d%d%d\n",&W,&H,&N); if(W==0) break; rep(i,H) getline(cin,s[i]); ax=0,ay=0,bx=0,by=0,cx=0,cy=0; rep(i,H) rep(j,W){ if(s[i][j]=='A') Ax=i,Ay=j; if(s[i][j]=='B') Bx=i,By=j; if(s[i][j]=='C') Cx=i,Cy=j; if(s[i][j]=='a') ax=i,ay=j; if(s[i][j]=='b') bx=i,by=j; if(s[i][j]=='c') cx=i,cy=j; } if(N==3) three(); if(N==2) two(); if(N==1) one(); cout<<d[ax][ay][bx][by][cx][cy]<<endl; } }
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[5] = {1, 0, -1, 0, 0}; int dy[5] = {0, 1, 0, -1, 0}; typedef pair<int, int> P; const int MAX_L = 16; const int MAX_N = 3; struct State{ P p[MAX_N]; int d, f; State(P pp[MAX_N], int dd, int hh){ REP(i, MAX_N) p[i] = pp[i]; d = dd; f = dd + hh; } bool operator < (const State& s) const { if(f != s.f) return f > s.f; return d < s.d; } }; int W, H, N; string grid[MAX_L]; inline int make(const P p[MAX_N]){ int res = 0; REP(i, N) { res = res << 8 | (p[i].first << 4) | p[i].second; } return res; } unsigned short dist[1 << 24]; inline void add(const State& s, priority_queue<State>& que){ int k = make(s.p); if(dist[k] > s.d){ dist[k] = s.d; que.push(s); } } inline bool equal(P p[MAX_N], P q[MAX_N]){ REP(i, N) if(p[i] != q[i]) return false; return true; } inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H) && grid[y][x] != '#'; } inline bool valid(const State& s, const State& prev){ REP(i, N) if(!valid(s.p[i].first, s.p[i].second, W, H)) return false; FOR(i, 0, N) FOR(j, i + 1, N) if(s.p[i] == s.p[j]) return false; FOR(i, 0, N) FOR(j, i + 1, N) if(s.p[i] == prev.p[j] && s.p[j] == prev.p[i]) return false; return true; } int memo[16][16][16][16]; int calc_distance(P start, P goal){ int& dist = memo[start.first][start.second][goal.first][goal.second]; if(dist != -1) return dist; queue<P> que; que.push(goal); memo[goal.first][goal.second][goal.first][goal.second] = 0; while(!que.empty()){ P p = que.front(); que.pop(); REP(r, 4){ P q = p; q.first += dx[r]; q.second += dy[r]; if(!valid(q.first, q.second, W, H)) continue; if(memo[q.first][q.second][goal.first][goal.second] != -1) continue; memo[q.first][q.second][goal.first][goal.second] = memo[p.first][p.second][goal.first][goal.second] + 1; que.push(q); } } return dist; } void print(State s){ printf("d = %d h = %d f = %d ", s.d, s.f - s.d, s.f); REP(i, N) printf("(%d, %d) ", s.p[i].first, s.p[i].second); cout << endl; } int main(){ while(cin >> W >> H >> N && N){ memset(memo, -1, sizeof memo); REP(i, 1 << 24) dist[i] = 16 * 16 * 3; cin.ignore(); REP(y, H) getline(cin, grid[y]); P goal[MAX_N]; P start[MAX_N]; REP(y, H)REP(x, W){ if(islower(grid[y][x])){ int k = grid[y][x] - 'a'; grid[y][x] = ' '; start[k] = P(x, y); } if(isupper(grid[y][x])){ int k = grid[y][x] - 'A'; grid[y][x] = ' '; goal[k] = P(x, y); } } priority_queue<State> que; State init(start, 0, 1); add(init, que); while(!que.empty()){ State s = que.top(); que.pop(); //print(s); if(s.d == s.f){ cout << s.d << endl; break; } REP(SR, 5 * 5 * 5){ int R = SR; State next = s; REP(i, N){ int r = R % 5; R /= 5; next.p[i].first += dx[r]; next.p[i].second += dy[r]; } if(!valid(next, s)) continue; next.d ++; int h = 0; REP(i, N){ h = max(h, calc_distance(next.p[i], goal[i])); } next.f = next.d + h; assert(h < INF); add(next, que); } } } return 0; }
#include <bits/stdc++.h> #define ll long long #define var auto using std::cin; using std::cout; using std::endl; int starts[3]; int ends[3]; bool m[256]; int states[256*256*256]; bool solve(){ int w, h, n; cin >> w >> h >> n; if (w == 0) return false; starts[0] = 0; starts[1] = 0; starts[2] = 0; ends[0] = 0; ends[1] = 0; ends[2] = 0; for (int i = 0; i < 256*256*256; i++){ states[i] = 2147483647 / 2; } getchar(); for (int i = 0; i < h; i++){ std::string s; char ch; for (int j = 0; j < w; j++){ ch = getchar(); s += ch; if ('A' <= s[j] && s[j] <= 'C'){ starts[s[j] - 'A'] = i * w + j; } if ('a' <= s[j] && s[j] <= 'c'){ ends[s[j] - 'a'] = i * w + j; } m[i * w + j] = (s[j] != '#'); } getchar(); //cout << s << endl; } int amove[5]; int bmove[5]; int cmove[5]; int acnt; int bcnt; int ccnt; var add = [&](int& cnt, int mv[], int ind){ if (!m[ind]) return; mv[cnt] = ind; cnt++; }; var adds = [&](int& cnt, int mv[], int p){ add(cnt, mv, p); add(cnt, mv, p - 1); add(cnt, mv, p + 1); add(cnt, mv, p - w); add(cnt, mv, p + w); }; std::queue<int> st{}; var initind = (starts[2] << 16) | (starts[1] << 8) | starts[0]; st.push(initind); states[initind] = 0; if (n == 1){ while (!st.empty()){ var elem = st.front(); st.pop(); var nexttime = states[elem] + 1; var ap = elem & 255; //printf("a:%d, b:%d, c:%d ->\n", ap, bp, cp); acnt = 0; adds(acnt, amove, ap); for (int i = 0; i < acnt; i++){ var ab = amove[i]; if (states[ab] <= nexttime) continue; //printf(" a:%d, b:%d, c:%d\n", amove[i], bmove[j], cmove[k]); states[ab] = nexttime; st.push(ab); } } } else if (n == 2){ while (!st.empty()){ var elem = st.front(); st.pop(); var nexttime = states[elem] + 1; var ap = elem & 255; elem >>= 8; var bp = elem & 255; //printf("a:%d, b:%d, c:%d ->\n", ap, bp, cp); acnt = 0; bcnt = 0; adds(acnt, amove, ap); adds(bcnt, bmove, bp); for (int i = 0; i < acnt; i++){ for (int j = 0; j < bcnt; j++){ if (amove[i] == bmove[j] || (amove[i] == bp && bmove[j] == ap)) continue; var ab = (bmove[j] << 8) | amove[i]; if (states[ab] <= nexttime) continue; //printf(" a:%d, b:%d, c:%d\n", amove[i], bmove[j], cmove[k]); states[ab] = nexttime; st.push(ab); } } } } else{ while (!st.empty()){ var elem = st.front(); st.pop(); var nexttime = states[elem] + 1; var ap = elem & 255; elem >>= 8; var bp = elem & 255; elem >>= 8; var cp = elem & 255; //printf("a:%d, b:%d, c:%d ->\n", ap, bp, cp); acnt = 0; bcnt = 0; ccnt = 0; adds(acnt, amove, ap); adds(bcnt, bmove, bp); adds(ccnt, cmove, cp); for (int i = 0; i < acnt; i++){ for (int j = 0; j < bcnt; j++){ if (amove[i] == bmove[j] || (amove[i] == bp && bmove[j] == ap)) continue; var ab = (bmove[j] << 8) | amove[i]; for (int k = 0; k < ccnt; k++){ if (amove[i] == cmove[k] || (amove[i] == cp && cmove[k] == ap) || bmove[j] == cmove[k] || (bmove[j] == cp && cmove[k] == bp)) continue; var abc = (cmove[k] << 16) | ab; if (states[abc] <= nexttime) continue; //printf(" a:%d, b:%d, c:%d\n", amove[i], bmove[j], cmove[k]); states[abc] = nexttime; st.push(abc); } } } } } var endind = (ends[2] << 16) | (ends[1] << 8) | ends[0]; std::cout << states[endind] << std::endl; return true; } int main(){ while(solve()); }
#include<iostream> #include<queue> #include<cctype> #include<algorithm> using namespace std; struct S{ int p[3][2]; int t; }; int main(){ for(int w,h,n;cin>>w>>h>>n,w;){ cin.ignore(); char g[16][16]; S is={{},0}; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cin.get(g[i][j]); if(islower(g[i][j])){ auto x=g[i][j]-'a'; is.p[x][0]=i; is.p[x][1]=j; } } cin.ignore(); } queue<S> que; que.push(is); static bool p[1<<24]; fill(begin(p),end(p),false); for(;;){ S cs=que.front(); bool f=false; for(int i=0;i<n;i++){ f|=g[cs.p[i][0]][cs.p[i][1]]!='A'+i; } if(!f)break; que.pop(); // int b=0; // for(int i=0;i<n;i++){ // b=b<<8|cs.p[i][0]<<4|cs.p[i][1]; // } // if(!p.insert(b).second)continue; static int dy[]={-1,0,0,0,1}; static int dx[]={0,-1,0,1,0}; for(int i=0;i<5;i++){ int y1,x1,y2,x2,y3,x3; auto enqueue=[&](){ S ns{{y1,x1,y2,x2,y3,x3},cs.t+1}; int b=0; for(int i=0;i<n;i++){ b=b<<8|ns.p[i][0]<<4|ns.p[i][1]; } if(!p[b]++){ que.push(ns); } }; y1=cs.p[0][0]+dy[i]; x1=cs.p[0][1]+dx[i]; y2=cs.p[1][0]; x2=cs.p[1][1]; y3=cs.p[2][0]; x3=cs.p[2][1]; if(g[y1][x1]=='#')continue; if(n==1){ enqueue(); }else{ for(int j=0;j<5;j++){ y2=cs.p[1][0]+dy[j]; x2=cs.p[1][1]+dx[j]; if(g[y2][x2]=='#'||y2==y1&&x2==x1||y1==cs.p[1][0]&&x1==cs.p[1][1]&&y2==cs.p[0][0]&&x2==cs.p[0][1])continue; if(n==2){ enqueue(); }else{ for(int k=0;k<5;k++){ y3=cs.p[2][0]+dy[k]; x3=cs.p[2][1]+dx[k]; if(g[y3][x3]=='#'||y3==y1&&x3==x1||y3==y2&&x3==x2||y3==cs.p[0][0]&&x3==cs.p[0][1]&&y1==cs.p[2][0]&&x1==cs.p[2][1]||y3==cs.p[1][0]&&x3==cs.p[1][1]&&y2==cs.p[2][0]&&x2==cs.p[2][1])continue; enqueue(); } } } } } } cout<<que.front().t<<endl; } }
#include <iostream> #include <queue> #include <string> #define inf 1000000000 using namespace std; struct state{ int x[3], y[3]; state(){} state(int a, int b, int c, int d, int e, int f){ x[0] = a, y[0] = b, x[1] = c, y[1] = d, x[2] = e, y[2] = f; } }; int w, h, n; char map[20][20]; int sx[3], sy[3]; int gx[3], gy[3]; int dist[14][15][14][15][14][15]; const int dx[] = {1, 0, -1, 0, 0}, dy[] = {0, -1, 0, 1, 0}; void bfs() { for(int ax = 0; ax < 14; ax++){ for(int ay = 0; ay < 15; ay++){ for(int bx = 0; bx < 14; bx++){ for(int by = 0; by < 15; by++){ for(int cx = 0; cx < 14; cx++){ for(int cy = 0; cy < 15; cy++){ dist[ax][ay][bx][by][cx][cy] = inf; } } } } } } dist[sx[0]][sy[0]][sx[1]][sy[1]][sx[2]][sy[2]] = 0; queue<state> Q; Q.push( state(sx[0], sy[0], sx[1], sy[1], sx[2], sy[2]) ); state st; int x[3], y[3], nx[3], ny[3]; while(Q.size()){ st = Q.front(); Q.pop(); for(int i = 0; i < 3; i++) x[i] = st.x[i], y[i] = st.y[i]; for(int ad = 0; ad < 5; ad++){ nx[0] = x[0] + dx[ad], ny[0] = y[0] + dy[ad]; if(y[0] < 14 && (nx[0] < 0 || nx[0] >= w || ny[0] < 0 || ny[0] >= h)) continue; if(map[nx[0]][ny[0]] == '#') continue; for(int bd = 0; bd < 5; bd++){ if(y[1] == 14 && bd < 4) continue; nx[1] = x[1] + dx[bd], ny[1] = y[1] + dy[bd]; if(y[1] < 14 && (nx[1] < 0 || nx[1] >= w || ny[1] < 0 || ny[1] >= h)) continue; if(map[nx[1]][ny[1]] == '#') continue; for(int cd = 0; cd < 5; cd++){ if(y[2] == 14 && cd < 4) continue; nx[2] = x[2] + dx[cd], ny[2] = y[2] + dy[cd]; if(y[2] < 14 && (nx[2] < 0 || nx[2] >= w || ny[2] < 0 || ny[2] >= h)) continue; if(map[nx[2]][ny[2]] == '#') continue; if(nx[0] == nx[1] && ny[0] == ny[1] && ny[1] != 14) continue; if(nx[1] == nx[2] && ny[1] == ny[2] && ny[2] != 14) continue; if(nx[2] == nx[0] && ny[2] == ny[0] && ny[0] != 14) continue; if(nx[0] == x[1] && ny[0] == y[1] && x[0] == nx[1] && y[0] == ny[1] && ny[1] != 14) continue; if(nx[1] == x[2] && ny[1] == y[2] && x[1] == nx[2] && y[1] == ny[2] && ny[2] != 14) continue; if(nx[2] == x[0] && ny[2] == y[0] && x[2] == nx[0] && y[2] == ny[0] && ny[0] != 14) continue; if(dist[nx[0]][ny[0]][nx[1]][ny[1]][nx[2]][ny[2]] < inf) continue; dist[nx[0]][ny[0]][nx[1]][ny[1]][nx[2]][ny[2]] = dist[x[0]][y[0]][x[1]][y[1]][x[2]][y[2]] + 1; Q.push( state(nx[0], ny[0], nx[1], ny[1], nx[2], ny[2]) ); } } } } } int main(void) { while(1){ cin >> w >> h >> n; if(w == 0 && h == 0 && n == 0) break; cin.ignore(); for(int i = 0; i < 3; i++){ sx[i] = gx[i] = 13; sy[i] = gy[i] = 14; } char c; string s; for(int y = 0; y < h; y++){ getline(cin, s); for(int x = 0; x < w; x++){ c = s[x]; if(x == 0 || x == w-1 || y == 0 || y == h-1) continue; if(c >= 'a' && c <= 'c'){ sx[c - 'a'] = x-1, sy[c - 'a'] = y-1; c = ' '; } if(c >= 'A' && c <= 'C'){ gx[c - 'A'] = x-1, gy[c - 'A'] = y-1; c = ' '; } map[x-1][y-1] = c; } } w -= 2, h -= 2; bfs(); cout << dist[gx[0]][gy[0]][gx[1]][gy[1]][gx[2]][gy[2]] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} using pi = pair<int,int>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,-1,0,1}; struct State{ int pa,pb,pc; }; const int N = 16*16; const int INF = 19191919; int dp[N][N][N]; int solve(int w, int h, int n){ cin.ignore(); vector<string> f(h); rep(i,h) getline(cin, f[i]); auto cv = [&](pi p){ return p.fi*w+p.se; }; int sa=0,sb=0,sc=0,ga=0,gb=0,gc=0; rep(i,h)rep(j,w){ if(f[i][j]=='a') sa = cv({i,j}); if(f[i][j]=='b') sb = cv({i,j}); if(f[i][j]=='c') sc = cv({i,j}); if(f[i][j]=='A') ga = cv({i,j}); if(f[i][j]=='B') gb = cv({i,j}); if(f[i][j]=='C') gc = cv({i,j}); } auto cand = [&](int p){ int y = p/w, x = p%w; vector<int> ret; ret.pb(p); rep(d,4){ int ny = y+dy[d], nx = x+dx[d]; if(0<=ny && ny<h && 0<=nx && nx<w && f[ny][nx]!='#') ret.pb(cv({ny,nx})); } return ret; }; rep(i,N)rep(j,N)rep(k,N) dp[i][j][k] = INF; dp[sa][sb][sc] = 0; queue<State> que; que.push({sa,sb,sc}); while(!que.empty()){ State now = que.front(); que.pop(); vector<int> da = cand(now.pa), db = cand(now.pb), dc = cand(now.pc); for(int A:da)for(int B:db)for(int C:dc){ if(B != 0){ if(B == A) continue; if(now.pa == B && now.pb == A) continue; } if(C != 0){ if(C == A || C == B) continue; if(now.pa == C && now.pc == A) continue; if(now.pb == C && now.pc == B) continue; } if(dp[A][B][C] > dp[now.pa][now.pb][now.pc]+1){ dp[A][B][C] = dp[now.pa][now.pb][now.pc]+1; if(A==ga && B==gb && C==gc) return dp[A][B][C]; que.push({A,B,C}); } } } assert(false); } int main(){ int w,h,n; while(cin >>w >>h >>n,w){ cout << solve(w,h,n) << endl; } return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define all(v) v.begin(),v.end() const int INF = 1 << 30; const int dx[] = {0, 1, 0, -1, 0}; const int dy[] = {1, 0, -1, 0, 0}; struct State { array<int, 3> xs, ys; State() { fill(all(xs), 0); fill(all(ys), 0); } int hash() { int a = 0; rep(i, 3) a = a | (xs[i] << (4 * i)); rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3)); return a; } }; array<short, 1 << 24> dist; bool solve() { int W, H, N; vector<string> G; cin >> W >> H >> N; if(W == 0) return false; cin.ignore(); rep(i, H) { string s; getline(cin, s); G.push_back(s); } State init, last; rep(y, H) rep(x, W) { int i = G[y][x] - 'a'; int j = G[y][x] - 'A'; if(0 <= i and i < N) { init.ys[i] = y; init.xs[i] = x; } if(0 <= j and j < N) { last.ys[j] = y; last.xs[j] = x; } } auto on_field = [&] (int y, int x) { return 0 <= y and y < H and 0 <= x and x < W and G[y][x] !='#'; }; auto move = [&] (State &s, int idx, int dir) { s.xs[idx] += dx[dir]; s.ys[idx] += dy[dir]; return on_field(s.ys[idx], s.xs[idx]); }; auto same = [&] (State &s, int i) { rep(j, i) { if(s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j]) return true; } return false; }; auto swapped = [&] (State &n, State &p) { rep(i, N) rep(j, N) if(i != j) { if((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and (n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j])) return true; } return false; }; int last_hash = last.hash(); queue<State> que; fill(all(dist), -1); que.push(init); dist[init.hash()] = 0; while(que.size()) { State now = que.front(); que.pop(); int nh = now.hash(); rep(i, 5) { State nxt1 = now; if(not move(nxt1, 0, i)) continue; rep(j, 5) { State nxt2 = nxt1; if(N >= 2 and not move(nxt2, 1, j)) continue; if(N >= 2 and same(nxt2, 1)) continue; rep(k, 5) { State nxt3 = nxt2; if(N >= 3 and not move(nxt3, 2, k)) continue; if(N >= 3 and same(nxt3, 2)) continue; if(not swapped(nxt3, now)) { int h = nxt3.hash(); if(last_hash == h) { cout << dist[nh] + 1 << endl; return 1; } if(dist[h] < 0) { dist[h] = dist[nh] + 1; que.push(nxt3); } } } } } } assert(0); return 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); while(solve()); return 0; }
#include <bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; struct P{ int ay,ax,by,bx,cy,cx,cost; P(int a1,int a2,int b1,int b2,int c1,int c2,int c){ ax=a1;ay=a2; bx=b1;by=b2; cx=c1;cy=c2; cost=c; } }; int h,w,n,gx[3],gy[3],sx[3],sy[3]; string s[14],iranai; bool used[14][14][14][14][14][14]; int dx[]={1,0,-1,0,0}; int dy[]={0,1,0,-1,0}; int BFS(){ memset(used,0,sizeof(used)); queue<P>q; q.push(P(sx[0],sy[0],sx[1],sy[1],sx[2],sy[2],0)); used[sy[0]][sx[0]][sy[1]][sx[1]][sy[2]][sx[2]]=1; while(!q.empty()){ P p=q.front();q.pop(); int ay=p.ay; int ax=p.ax; int by=p.by; int bx=p.bx; int cy=p.cy; int cx=p.cx; int cost=p.cost; if(ay==gy[0]&&ax==gx[0]&&by==gy[1]&&bx==gx[1]&&cy==gy[2]&&cx==gx[2])return cost; for(int i=0;i<5;i++){ int Ay=ay+dy[i]; int Ax=ax+dx[i]; if(Ay<0||Ax<0||Ay>=h||Ax>=w)continue; if(s[Ay][Ax]=='#')continue; if(n>=2){ for(int j=0;j<5;j++){ int By=by+dy[j]; int Bx=bx+dx[j]; if(By<0||Bx<0||By>=h||Bx>=w)continue; if(s[By][Bx]=='#')continue; if(n>=3){ for(int j=0;j<5;j++){ int Cy=cy+dy[j]; int Cx=cx+dx[j]; if(Cy<0||Cx<0||Cy>=h||Cx>=w)continue; if(s[Cy][Cx]=='#')continue; if(Ax==bx&&Ay==by&&Bx==ax&&By==ay)continue; if(Cx==bx&&Cy==by&&Bx==cx&&By==cy)continue; if(Ax==cx&&Ay==cy&&Cx==ax&&Cy==ay)continue; if(Ax==Bx&&Ay==By)continue; if(Ax==Cx&&Ay==Cy)continue; if(Cx==Bx&&Cy==By)continue; if(!used[Ay][Ax][By][Bx][Cy][Cx]){ used[Ay][Ax][By][Bx][Cy][Cx]=1; q.push(P(Ax,Ay,Bx,By,Cx,Cy,cost+1)); } } } else{ if(Ax==bx&&Ay==by&&Bx==ax&&By==ay)continue; if(Ax==Bx&&Ay==By)continue; if(!used[Ay][Ax][By][Bx][cy][cx]){ used[Ay][Ax][By][Bx][cy][cx]=1; q.push(P(Ax,Ay,Bx,By,cx,cy,cost+1)); } } } } else{ if(!used[Ay][Ax][by][bx][cy][cx]){ used[Ay][Ax][by][bx][cy][cx]=1; q.push(P(Ax,Ay,bx,by,cx,cy,cost+1)); } } } } return -1; } int main(){ while(cin>>w>>h>>n,n){ memset(sy,0,sizeof(sy)); memset(sx,0,sizeof(sx)); memset(gx,0,sizeof(gx)); memset(gy,0,sizeof(gy)); getline(cin,s[0]); getline(cin,s[0]); r(i,h-2)getline(cin,s[i]); getline(cin,iranai); h-=2;w-=2; r(i,h)s[i].erase(s[i].begin(),s[i].begin()+1); r(i,h)s[i].erase(s[i].begin()+w); r(i,h)r(j,w){ if(s[i][j]=='a')sy[0]=i,sx[0]=j; if(s[i][j]=='b')sy[1]=i,sx[1]=j; if(s[i][j]=='c')sy[2]=i,sx[2]=j; if(s[i][j]=='A')gy[0]=i,gx[0]=j; if(s[i][j]=='B')gy[1]=i,gx[1]=j; if(s[i][j]=='C')gy[2]=i,gx[2]=j; } cout<<BFS()<<endl; } }
#include <array> #include <cassert> #include <cctype> #include <cstdlib> #include <functional> #include <iostream> #include <queue> #include <tuple> #include <vector> using namespace std; typedef vector<vector<int>> graph; enum { A, B, C, N }; inline bool ng(int current_a, int next_a, int current_b, int next_b) { return next_a == next_b || (current_a == next_b && next_a == current_b); } inline int bfs(const array<int, N> &start, const array<int, N> &goal, const graph &G) { const int V = G.size(); vector<vector<vector<int>>> dist(V, vector<vector<int>>(V, vector<int>(V, -1))); queue<tuple<int, int, int>> que; dist[start[A]][start[B]][start[C]] = 0; que.push(make_tuple(start[A], start[B], start[C])); while(!que.empty()) { int a, b, c; tie(a, b, c) = que.front(); que.pop(); const int d = dist[a][b][c]; for(const auto &to_a : G[a]) { for(const auto &to_b : G[b]) { if(ng(a, to_a, b, to_b)) continue; for(const auto &to_c : G[c]) { if(ng(a, to_a, c, to_c) || ng(b, to_b, c, to_c)) continue; if(dist[to_a][to_b][to_c] == -1) { if(to_a == goal[A] && to_b == goal[B] && to_c == goal[C]) return d + 1; dist[to_a][to_b][to_c] = d + 1; que.push(make_tuple(to_a, to_b, to_c)); } } } } } assert(false); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for(int w, h, n; cin >> w >> h >> n && w;) { cin.ignore(); vector<string> field(h); for(auto &e : field) getline(cin, e); int num = 0; array<int, N> start, goal; vector<vector<int>> label(h, vector<int>(w, -1)); for(int i = 1; i < h - 1; ++i) { for(int j = 1; j < w - 1; ++j) { if(field[i][j] != '#') { if(islower(field[i][j])) { start[field[i][j] - 'a'] = num; } else if(isupper(field[i][j])) { goal[field[i][j] - 'A'] = num; } label[i][j] = num++; } } } for(int i = n; i < N; ++i) { start[i] = goal[i] = num++; } graph G(num); for(int v = 0; v < num; ++v) G[v].emplace_back(v); for(int i = 1; i < h - 1; ++i) { for(int j = 1; j < w - 1; ++j) { if(label[i][j] == -1) continue; const int v = label[i][j]; if(label[i - 1][j] != -1) { const int u = label[i - 1][j]; G[v].emplace_back(u); G[u].emplace_back(v); } if(label[i][j - 1] != -1) { const int u = label[i][j - 1]; G[v].emplace_back(u); G[u].emplace_back(v); } } } cout << bfs(start, goal, G) << endl; } return EXIT_SUCCESS; }
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; void solve(int w,int h,int n){ vector<int >v[200]; int cur=0; vector<vector<int>>id(h,vector<int>(w)); string s[h]; rep(i,h){ getline(cin,s[i]); rep(j,w){ if(s[i][j]!='#')id[i][j]=cur++; } } rep(i,h)rep(j,w-1){ if(s[i][j]!='#'&&s[i][j+1]!='#'){ v[id[i][j]].push_back(id[i][j+1]); v[id[i][j+1]].push_back(id[i][j]); } } rep(i,h-1)rep(j,w){ if(s[i][j]!='#'&&s[i+1][j]!='#'){ v[id[i][j]].push_back(id[i+1][j]); v[id[i+1][j]].push_back(id[i][j]); } } rep(i,cur)v[i].push_back(i); int m=1; int st=0,gl=0; rep(i,n){ m*=cur; rep(j,h)rep(k,w){ if(s[j][k]=='a'+i)st=st*cur+id[j][k]; if(s[j][k]=='A'+i)gl=gl*cur+id[j][k]; } } vector<int> dist(m,inf); queue<int> que; que.push(st); dist[st]=0; vector<int> pos(n),nxt(n); while(que.size()){ int p=que.front();que.pop(); int pp=p; rep(i,n){ pos[n-i-1]=pp%cur; pp/=cur; } if(n==1){ for(auto to : v[pos[0]]){ if(dist[to]>dist[p]+1){ dist[to]=dist[p]+1; que.push(to); } } } else if(n==2){ for(auto to0 : v[pos[0]])for(auto to1 : v[pos[1]]){ if(to0==to1)continue; if(to0==pos[1]&&to1==pos[0])continue; int nxt=to0*cur+to1; if(dist[nxt]>dist[p]+1){ dist[nxt]=dist[p]+1; que.push(nxt); } } } else { for(auto to0 : v[pos[0]])for(auto to1 : v[pos[1]])for(auto to2 : v[pos[2]]){ if(to0==to1||to1==to2||to2==to0)continue; if(to0==pos[1]&&to1==pos[0])continue; if(to0==pos[2]&&to2==pos[0])continue; if(to1==pos[2]&&to2==pos[1])continue; int nxt=to0*cur*cur+to1*cur+to2; if(dist[nxt]>dist[p]+1){ dist[nxt]=dist[p]+1; que.push(nxt); } } } } cout<<dist[gl]<<endl; } int main(){ int w,h,n; while(1){ scanf("%d%d%d ",&w,&h,&n); if(w==0)break; solve(w,h,n); } return 0; }
#include<stdio.h> #include<string.h> #include<bitset> #include<queue> char m[32][32]; const int dx[]={0,1,0,-1,0},dy[]={1,0,-1,0,0}; std::bitset<1<<24 >f; template<int N> struct S { void I2P(int a,int*x,int*y) { for(int i=0;i<N;++i) { y[N-i-1]=a&15; x[N-i-1]=(a>>4)&15; a>>=8; } } int P2I(int*x,int*y) { int res=0,i; for(i=0;i<N;++i) res=(res<<8)|(x[i]<<4)+y[i]; return res; } int EQ(int*x,int*y,int*gx,int*gy) { for(int i=0;i<N;++i) if(x[i]!=gx[i]||y[i]!=gy[i]) return 0; return 1; } void G(std::queue<int>&q, int*x,int*y,int*nx,int*ny,int/* i*/) { int i,j,k,a; for(i=0;i<5;++i) { nx[0]=x[0]+dx[i]; ny[0]=y[0]+dy[i]; if(m[ny[0]][nx[0]]=='#')continue; if(N==1) { a=P2I(nx,ny); if(!f[a]) q.push(a); continue; } for(j=0;j<5;++j) { nx[1]=x[1]+dx[j]; ny[1]=y[1]+dy[j]; if(m[ny[1]][nx[1]]=='#' || nx[0]==nx[1]&&ny[0]==ny[1])continue; if(x[0]==nx[1]&&y[0]==ny[1]&&x[1]==nx[0]&&y[1]==ny[0]) continue; if(N==2) { a=P2I(nx,ny); if(!f[a]) q.push(a); continue; } for(k=0;k<5;++k) { nx[2]=x[2]+dx[k]; ny[2]=y[2]+dy[k]; if(m[ny[2]][nx[2]]=='#' || nx[0]==nx[2]&&ny[0]==ny[2] || nx[1]==nx[2]&&ny[1]==ny[2])continue; if(x[0]==nx[2]&&y[0]==ny[2]&&x[2]==nx[0]&&y[2]==ny[0] || x[1]==nx[2]&&y[1]==ny[2]&&x[2]==nx[1]&&y[2]==ny[1]) continue; a=P2I(nx,ny); if(!f[a]) q.push(a); } } } } int F(int (&x)[3],int (&y)[3],int (&gx)[3],int(&gy)[3]) { f.reset(); std::queue<int>q; q.push(P2I(x,y)); q.push(-1); int a,res=0; int nx[3],ny[3]; for(;;) { a=q.front(); q.pop(); if(a<0) { ++res; q.push(a); continue; } if(f[a])continue; f[a]=true; I2P(a,x,y); if(EQ(x,y,gx,gy))return res; G(q,x,y,nx,ny,0); } } }; int main() { int h,w,n,i,x[3],y[3],gx[3],gy[3]; char*p; while(scanf("%d%d%d",&w,&h,&n),n) { fgets(m[0],32,stdin); for(i=0;i<h;++i) { fgets(m[i],32,stdin); for(p=m[i];p=strpbrk(p,"abc");++p) { y[*p-'a']=i; x[*p-'a']=p-m[i]; } for(p=m[i];p=strpbrk(p,"ABC");++p) { gy[*p-'A']=i; gx[*p-'A']=p-m[i]; } } int r = (n==3?S<3>().F(x,y,gx,gy): n==2?S<2>().F(x,y,gx,gy): S<1>().F(x,y,gx,gy)); printf("%d\n",r); } return 0; }
#include <bits/stdc++.h> #define _overload(_1,_2,_3,name,...) name #define _rep(i,n) _range(i,0,n) #define _range(i,a,b) for(int i=int(a);i<int(b);++i) #define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__) #define _rrep(i,n) _rrange(i,n,0) #define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i) #define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__) #define _all(arg) begin(arg),end(arg) #define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg)) #define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary) #define clr(a,b) memset((a),(b),sizeof(a)) #define bit(n) (1LL<<(n)) #define popcount(n) (__builtin_popcountll(n)) template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;} template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;} using namespace std; const int dx[8]={1,0,-1,0,0}; const int dy[8]={0,1,0,-1,0}; const int limit=1<<24; using S=short int; S pre[3][limit]; S dist[limit]; inline int hstar(int n,int cmask,int tmask){ int ret=0; rep(i,n){ int cur=(cmask>>(8*i))&255; chmax<int>(ret,pre[i][cur]); } return ret; } int main(void){ int w,h,n; while(cin >> w >> h >> n,n){ int smask=0,tmask=0; string board[16]; cin.ignore(); rep(i,h) getline(cin,board[i]); // cout << w << " " << h << " " << n << endl; // rep(i,h) cout << board[i] << endl; rep(i,h)rep(j,w)rep(k,3){ if(board[i][j]==('a'+k)) smask|=((16*i+j)<<(8*k)); if(board[i][j]==('A'+k)) tmask|=((16*i+j)<<(8*k)); } rep(i,3){ fill(pre[i],pre[i]+256,300); const int start=(tmask>>(8*i))&255; pre[i][start]=0; queue<int> q; q.push(start); while(!q.empty()){ int cmask=q.front();q.pop(); const int cy=cmask/16,cx=cmask%16; rep(j,4){ const int ny=cy+dy[j],nx=cx+dx[j]; if(board[ny][nx]=='#') continue; if(chmin<S>(pre[i][16*ny+nx],pre[i][16*cy+cx]+1)) q.push(16*ny+nx); } } } fill(dist,dist+limit,30000); dist[smask]=0; using state=tuple<S,int>; priority_queue<state,vector<state>,greater<state>> q; q.push(state(0,smask)); while(!q.empty()){ int cost,cmask; tie(cost,cmask)=q.top();q.pop(); if(dist[tmask]!=30000) break; int cy[3],cx[3],tmp=cmask; rep(i,n){ cx[i]=tmp&15; tmp>>=4; cy[i]=tmp&15; tmp>>=4; } const int all=pow<int>(5,n); rep(mask,all){ const int didx[3]={mask%5,mask/5%5,mask/25}; int ny[3],nx[3]; rep(i,n){ ny[i]=cy[i]+dy[didx[i]]; nx[i]=cx[i]+dx[didx[i]]; } bool ok=true; rep(i,n) if(board[ny[i]][nx[i]]=='#') ok=false; if(ok==false) continue; rep(i,n)rep(j,i) if(ny[i]==ny[j] && nx[i]==nx[j] ) ok=false; if(ok==false) continue; rep(i,n)rep(j,i) if(ny[i]==cy[j] && nx[i]==cx[j] && ny[j]==cy[i] && nx[j]==cx[i]) ok=false; if(ok==false) continue; int nmask=0; rep(i,n) nmask|=(16*ny[i]+nx[i])<<(8*i); if(chmin<S>(dist[nmask],dist[cmask]+1)) q.push(state(dist[nmask]+hstar(n,nmask,tmask),nmask)); } } cout << dist[tmask] << endl; } return 0; }
#pragma GCC optimize "O3" #include <bits/stdc++.h> using namespace std; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define all(v) v.begin(),v.end() const int INF = 1 << 30; const int dx[] = {0, 1, 0, -1, 0}; const int dy[] = {1, 0, -1, 0, 0}; struct State { array<int, 3> xs, ys; State() { fill(all(xs), 0); fill(all(ys), 0); } int hash() { int a = 0; rep(i, 3) a = a | (xs[i] << (4 * i)); rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3)); return a; } }; array<short, 1 << 24> dist; bool solve() { int W, H, N; vector<string> G; cin >> W >> H >> N; if(W == 0) return false; cin.ignore(); rep(i, H) { string s; getline(cin, s); G.push_back(s); } State init, last; rep(y, H) rep(x, W) { int i = G[y][x] - 'a'; int j = G[y][x] - 'A'; if(0 <= i and i < N) { init.ys[i] = y; init.xs[i] = x; } if(0 <= j and j < N) { last.ys[j] = y; last.xs[j] = x; } } auto on_field = [&] (int y, int x) { return 0 <= y and y < H and 0 <= x and x < W and G[y][x] !='#'; }; auto move = [&] (State &s, int idx, int dir) { s.xs[idx] += dx[dir]; s.ys[idx] += dy[dir]; return on_field(s.ys[idx], s.xs[idx]); }; auto same = [&] (State &s, int i) { rep(j, i) { if(s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j]) return true; } return false; }; auto swapped = [&] (State &n, State &p) { rep(i, N) rep(j, N) if(i != j) { if((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and (n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j])) return true; } return false; }; int last_hash = last.hash(); queue<State> que; fill(all(dist), -1); que.push(init); dist[init.hash()] = 0; while(que.size()) { State now = que.front(); que.pop(); int nh = now.hash(); rep(i, 5) { State nxt1 = now; if(not move(nxt1, 0, i)) continue; rep(j, 5) { State nxt2 = nxt1; if(N >= 2 and not move(nxt2, 1, j)) continue; if(N >= 2 and same(nxt2, 1)) continue; rep(k, 5) { State nxt3 = nxt2; if(N >= 3 and not move(nxt3, 2, k)) continue; if(N >= 3 and same(nxt3, 2)) continue; if(not swapped(nxt3, now)) { int h = nxt3.hash(); if(last_hash == h) { cout << dist[nh] + 1 << endl; return 1; } if(dist[h] < 0) { dist[h] = dist[nh] + 1; que.push(nxt3); } } } } } } assert(0); return 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); while(solve()); return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("avx2") #include <bits/stdc++.h> using namespace std; #define rep(i, j) for(int i=0; i < (int)(j); i++) #define all(v) v.begin(),v.end() const int INF = 1 << 30; const int dx[] = {0, 1, 0, -1, 0}; const int dy[] = {1, 0, -1, 0, 0}; struct State { array<int, 3> xs, ys; State() { fill(all(xs), 0); fill(all(ys), 0); } int hash() { int a = 0; rep(i, 3) a = a | (xs[i] << (4 * i)); rep(i, 3) a = a | (ys[i] << (4 * i + 4 * 3)); return a; } }; array<short, 1 << 24> dist; bool solve() { int W, H, N; vector<string> G; cin >> W >> H >> N; if(W == 0) return false; cin.ignore(); rep(i, H) { string s; getline(cin, s); G.push_back(s); } State init, last; rep(y, H) rep(x, W) { int i = G[y][x] - 'a'; int j = G[y][x] - 'A'; if(0 <= i and i < N) { init.ys[i] = y; init.xs[i] = x; } if(0 <= j and j < N) { last.ys[j] = y; last.xs[j] = x; } } auto on_field = [&] (int y, int x) { return 0 <= y and y < H and 0 <= x and x < W and G[y][x] !='#'; }; auto move = [&] (State &s, int idx, int dir) { s.xs[idx] += dx[dir]; s.ys[idx] += dy[dir]; return on_field(s.ys[idx], s.xs[idx]); }; auto same = [&] (State &s, int i) { rep(j, i) { if(s.ys[i] == s.ys[j] and s.xs[i] == s.xs[j]) return true; } return false; }; auto swapped = [&] (State &n, State &p) { rep(i, N) rep(j, N) if(i != j) { if((n.ys[j] == p.ys[i] and n.xs[j] == p.xs[i]) and (n.ys[i] == p.ys[j] and n.xs[i] == p.xs[j])) return true; } return false; }; int last_hash = last.hash(); queue<State> que; fill(all(dist), -1); que.push(init); dist[init.hash()] = 0; while(que.size()) { State now = que.front(); que.pop(); int nh = now.hash(); rep(i, 5) { State nxt1 = now; if(not move(nxt1, 0, i)) continue; rep(j, 5) { State nxt2 = nxt1; if(N >= 2 and not move(nxt2, 1, j)) continue; if(N >= 2 and same(nxt2, 1)) continue; rep(k, 5) { State nxt3 = nxt2; if(N >= 3 and not move(nxt3, 2, k)) continue; if(N >= 3 and same(nxt3, 2)) continue; if(not swapped(nxt3, now)) { int h = nxt3.hash(); if(last_hash == h) { cout << dist[nh] + 1 << endl; return 1; } if(dist[h] < 0) { dist[h] = dist[nh] + 1; que.push(nxt3); } } } } } } assert(0); return 1; } int main() { cin.tie(0); ios::sync_with_stdio(false); while(solve()); return 0; }
#include <iostream> #include <array> #include <vector> #include <cctype> #include <map> #include <queue> #include <cassert> #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) using namespace std; struct point_t { int y, x; }; point_t operator + (point_t const & a, point_t const & b) { return (point_t){ a.y + b.y, a.x + b.x }; } bool operator == (point_t const & a, point_t const & b) { return make_pair(a.y, a.x) == make_pair(b.y, b.x); } bool operator < (point_t const & a, point_t const & b) { return make_pair(a.y, a.x) < make_pair(b.y, b.x); } static point_t dp[5] = { {-1,0}, {1,0}, {0,1}, {0,-1}, {0,0} }; struct state_t { array<point_t,3> a; int cost; int dist; }; #define MAGIC_WEIGHT 1.3 bool operator < (state_t const & a, state_t const & b) { return MAGIC_WEIGHT * a.cost + a.dist > MAGIC_WEIGHT * b.cost + b.dist; } bool is_valid_move(int i, int j, array<point_t,3> const & s, array<point_t,3> const & t) { if (t[i] == t[j]) return false; if (t[i] == s[j] and t[j] == s[i]) return false; return true; } int main() { while (true) { int w, h, n; cin >> w >> h >> n; cin.ignore(); if (w == 0 and h == 0 and n == 0) break; assert (1 <= n and n <= 3); vector<string> c(h); repeat (i,h) getline(cin, c[i]); array<point_t,3> start; array<point_t,3> goal; map<point_t,int> ix; repeat (y,h) repeat (x,w) { if (islower(c[y][x])) { start[c[y][x] - 'a'] = { y, x }; } else if (isupper(c[y][x])) { goal[c[y][x] - 'A'] = { y, x }; } if (c[y][x] != '#') { int i = ix.size(); ix[(point_t){ y, x }] = i; } } repeat_from (i,n,3) { start[i] = ix.begin()->first; // fill with some non-wall cell goal[i] = ix.begin()->first; } vector<vector<int> > dist[3]; // from goal repeat (i,3) { dist[i].resize(h, vector<int>(w, 1000000007)); queue<point_t> que; // bfs que.push(goal[i]); dist[i][goal[i].y][goal[i].x] = 0; while (not que.empty()) { point_t p = que.front(); que.pop(); repeat (j,4) { auto q = p + dp[j]; if (c[q.y][q.x] == '#') continue; if (dist[i][q.y][q.x] == 1000000007) { dist[i][q.y][q.x] = dist[i][p.y][p.x] + 1; que.push(q); } } } } vector<vector<vector<bool> > > used(ix.size(), vector<vector<bool> >(ix.size(), vector<bool>(ix.size()))); priority_queue<state_t> que; { state_t initial = { start, 0, 0 }; repeat (i,3) initial.dist += dist[i][start[i].y][start[i].x]; used[ix[start[0]]][ix[start[1]]][ix[start[2]]] = true; que.push(initial); } while (not que.empty()) { state_t st = que.top(); que.pop(); array<point_t,3> & s = st.a; if (s == goal) { cout << st.cost << endl; break; } state_t tt = st; tt.cost += 1; array<point_t,3> & t = tt.a; repeat (i,5) { // a t[0] = s[0] + dp[i]; if (c[t[0].y][t[0].x] == '#') continue; repeat (j,5) { // b if (n < 2 and j != 4) continue; t[1] = s[1] + dp[j]; if (c[t[1].y][t[1].x] == '#') continue; if (not is_valid_move(0, 1, s, t)) continue; repeat (k,5) { // c if (n < 3 and k != 4) continue; t[2] = s[2] + dp[k]; if (c[t[2].y][t[2].x] == '#') continue; if (n == 3 and not is_valid_move(1, 2, s, t)) continue; if (n == 3 and not is_valid_move(2, 0, s, t)) continue; if (used[ix[t[0]]][ix[t[1]]][ix[t[2]]]) continue; used[ix[t[0]]][ix[t[1]]][ix[t[2]]] = true; tt.dist = 0; repeat (i,3) tt.dist = max(tt.dist, dist[i][t[i].y][t[i].x]); que.push(tt); } } } } } return 0; }
#include <iostream> #include <queue> #include <string> #include <cstring> using namespace std; int w, h, n; string b[16]; int sx[3], sy[3], gx[3], gy[3]; short v[1 << 24]; short dist[3][16][16]; int dx[5] = { 0, 1, 0, -1, 0 }, dy[5] = { 0, 0, 1, 0, -1 }; struct S { unsigned char x[3], y[3]; float h; bool operator < (const S& s) const { return h > s.h; } }; float calcH(S& s) { float ret = 0; for(int i = 0; i < n; i++) { ret = max(ret, (float)dist[i][s.y[i]][s.x[i]]); } return 1.1 * ret; } int conv(S s) { int ret = 0; for(int i = 0; i < n; i++) { ret = ret * 16 + s.x[i]; } for(int i = 0; i < n; i++) { ret = ret * 16 + s.y[i]; } return ret; } bool check(S s) { for(int i = 0; i < n; i++) { if(s.x[i] != gx[i] || s.y[i] != gy[i]) return false; } return true; } bool in(int x, int y) { return 0 <= x && x < w && 0 <= y && y < h; } bool valid(S s, S ps) { for(int i = 0; i < n; i++) { if(!in(s.x[i], s.y[i])) return false; if(b[s.y[i]][s.x[i]] == '#') return false; } for(int i = 0; i < n; i++) { for(int j = i + 1; j < n; j++) { if(s.x[i] == s.x[j] && s.y[i] == s.y[j]) return false; if(ps.x[j] == s.x[i] && ps.y[j] == s.y[i] && ps.x[i] == s.x[j] && ps.y[i] == s.y[j]) return false; } } return true; } void view(S s) { string temp[16]; for(int i = 0; i < h; i++) { temp[i] = b[i]; } for(int i = 0; i < n; i++) { temp[s.y[i]][s.x[i]] = 'a' + i; } for(int i = 0; i < h; i++) { cout << temp[i] << endl; } cout << endl; } void makeNS(int i, S s, S& ps, vector<S>& res) { if(i == n) { if(valid(s, ps)) res.push_back(s); return; } for(int k = 0; k < 5; k++) { S ns = s; ns.x[i] += dx[k]; ns.y[i] += dy[k]; makeNS(i + 1, ns, ps, res); } } int main() { while(cin >> w >> h >> n, w | h | n) { cin.ignore(); S s; for(int i = 0; i < h; i++) { getline(cin, b[i]); for(int j = 0; j < w; j++) { if(b[i][j] == 'a') s.x[0] = j, s.y[0] = i; if(b[i][j] == 'b') s.x[1] = j, s.y[1] = i; if(b[i][j] == 'c') s.x[2] = j, s.y[2] = i; if(b[i][j] == 'A') gx[0] = j, gy[0] = i; if(b[i][j] == 'B') gx[1] = j, gy[1] = i; if(b[i][j] == 'C') gx[2] = j, gy[2] = i; if(b[i][j] == 'a' || b[i][j] == 'b' || b[i][j] == 'c') b[i][j] = ' '; } //cout << b[i] << endl; } memset(dist, -1, sizeof dist); for(int l = 0; l < n; l++) { for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(b[i][j] == '#') continue; queue<pair<int, int>> q; q.push(make_pair(gx[l], gy[l])); dist[l][gy[l]][gx[l]] = 0; while(q.size()) { int x = q.front().first, y = q.front().second; q.pop(); for(int k = 1; k < 5; k++) { int nx = x + dx[k], ny = y + dy[k]; if(b[ny][nx] == '#') continue; if(dist[l][ny][nx] != -1) continue; dist[l][ny][nx] = dist[l][y][x] + 1; q.push(make_pair(nx, ny)); } } } } } memset(v, -1, sizeof v); priority_queue<S> q; s.h = calcH(s); q.push(s); v[conv(s)] = 0; int ans = -1; while(q.size()) { s = q.top(); q.pop(); int d = v[conv(s)]; //cout << conv(s) << " " << d << endl; //view(s); if(check(s)) { ans = d; break; } vector<S> NS; makeNS(0, s, s, NS); for(S ns : NS) { int X = conv(ns); if(v[X] != -1 && v[X] <= d + 1) continue; v[X] = d + 1; ns.h = calcH(ns) + d + 1; q.push(ns); if(q.size() > 100000) q.pop(); } } cout << ans << endl; //return 0; } }
#include <cstdio> #include <queue> #include <cstring> #include <cctype> #include <algorithm> using namespace std; const int INF = 1000; const int MAX_W = 16; const int dy[] = {1,-1,0,0,0}, dx[] = {0,0,1,-1,0}; int W, H, N; int GY[3], GX[3], SY[3], SX[3]; char board[MAX_W][MAX_W + 1]; int distFromGoal[3][MAX_W][MAX_W]; unsigned short dist[MAX_W][MAX_W][MAX_W][MAX_W][MAX_W][MAX_W]; struct State { unsigned short dist; int y[3], x[3]; State(){} State(unsigned short dist, int y_[3], int x_[3]): dist(dist) { memcpy(y, y_, sizeof(y)); memcpy(x, x_, sizeof(x)); } }; bool operator> (const State& lhs, const State& rhs) { return lhs.dist > rhs.dist; } inline int getDist(int y0, int x0, int id) { return distFromGoal[id][y0][x0]; } inline int getDist(int y[3], int x[3]) { int ans = 0; for (int i = 0; i < N; ++i) { ans = max(ans, getDist(y[i], x[i], i)); } return ans; } bool init() { scanf("%d%d%d ", &W, &H, &N); for (int i = 0; i < H; ++i ) { scanf("%[^\n]%*c", board[i]); } return H > 0; } void dfs(int y[3], int x[3], int ny[3], int nx[3], int depth, int d, priority_queue<State, vector<State>, greater<State> > &que) { if (depth == N) { for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { if (ny[i] == ny[j] && nx[i] == nx[j]) { return ; } if (y[i] == ny[j] && x[i] == nx[j] && y[j] == ny[i] && x[j] == nx[i]) { return ; } } } const int nd = d - getDist(y, x) + getDist(ny, nx) + 1; if (nd < dist[ny[0]][nx[0]][ny[1]][nx[1]][ny[2]][nx[2]]) { dist[ny[0]][nx[0]][ny[1]][nx[1]][ny[2]][nx[2]] = nd; que.push(State(nd, ny, nx)); } return ; } for (int k = 0; k < 5; ++k) { ny[depth] = y[depth] + dy[k], nx[depth] = x[depth] + dx[k]; if ( 0 <= ny[depth] && ny[depth] < H && 0 <= nx[depth] && nx[depth] < W && board[ny[depth]][nx[depth]] != '#') { dfs(y, x, ny, nx, depth + 1, d, que); } } } int solve() { memset(GY, 0, sizeof(GY)); memset(GX, 0, sizeof(GX)); memset(SY, 0, sizeof(SY)); memset(SX, 0, sizeof(SX)); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (isupper(board[i][j])) { const int id = board[i][j] - 'A'; GY[id] = i; GX[id] = j; board[i][j] = ' '; } if (islower(board[i][j])) { const int id = board[i][j] - 'a'; SY[id] = i; SX[id] = j; board[i][j] = ' '; } } } for (int k = 0; k < N; ++k) { for (int i = 0; i < H; ++i) { fill(distFromGoal[k][i], distFromGoal[k][i] + W, INF); } } for (int i = 0; i < N; ++i) { queue<pair<int,int> > que; que.push(make_pair(GY[i], GX[i])); distFromGoal[i][GY[i]][GX[i]] = 0; for (;!que.empty();) { pair<int,int> tp = que.front(); que.pop(); const int y = tp.first, x = tp.second; for (int k = 0; k < 4; ++k) { const int ny = y + dy[k], nx = x + dx[k]; if ( 0 <= ny && ny < H && 0 <= nx && nx < W && board[ny][nx] != '#' && distFromGoal[i][ny][nx] == INF) { distFromGoal[i][ny][nx] = distFromGoal[i][y][x] + 1; que.push(make_pair(ny, nx)); } } } } memset(dist, ~0, sizeof(dist)); priority_queue<State, vector<State>, greater<State> > que; int initValue = getDist(SY, SX); que.push(State(initValue, SY, SX)); dist[SY[0]][SX[0]][SY[1]][SX[1]][SY[2]][SX[2]] = initValue; for (;!que.empty();) { State tp = que.top(); que.pop(); int y[3], x[3]; memcpy(y, tp.y, sizeof(y)); memcpy(x, tp.x, sizeof(x)); if (dist[y[0]][x[0]][y[1]][x[1]][y[2]][x[2]] < tp.dist) { continue; } { bool cut = true; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) if (i != j) { cut &= distFromGoal[i][y[i]][x[i]] < distFromGoal[i][y[j]][x[j]]; } } if (cut) { return int(tp.dist); } } int ny[3], nx[3]; memcpy(ny, tp.y, sizeof(ny)); memcpy(nx, tp.x, sizeof(nx)); dfs(y, x, ny, nx, 0, tp.dist, que); } return 0; } int main() { for (;init();) { printf("%d\n", solve()); } return 0; }
/* * D.cpp * * Created on: Oct 11, 2013 * Author: Hesham */ #include<iostream> #include<string> #include<map> #include<ctime> #include<vector> #include<queue> #include<stack> #include<set> #include<algorithm> #include<sstream> #include<iomanip> #include<cstring> #include<bitset> #include<fstream> #include<cmath> #include<cassert> #include <stdio.h> #include<ctype.h> using namespace std; int dx[] = { 0, 0, 1, -1, 0 }; int dy[] = { 0, 1, 0, 0, -1 }; int w, h, n; string house[16]; struct State { vector<int> y, x; State() { x.resize(3, 0); y.resize(3, 0); } State(vector<int>& yy, vector<int>& xx) : x(xx), y(yy) { } bool operator ==(const State& other) const { return x == other.x && y == other.y; } }; bool vis1[16][16][16][16][16][16]; bool vis2[16][16][16][16][16][16]; //short dist[16][16][16][16][16][16]; bool isVis(State& s, bool vis[16][16][16][16][16][16]) { return vis[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]]; } void setVis(State& s, bool vis[16][16][16][16][16][16]) { vis[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]] = true; } /*int getDist(State& s) { return dist[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]]; }*/ /*void setDist(State& s, int v) { dist[s.x[0]][s.x[1]][s.x[2]][s.y[0]][s.y[1]][s.y[2]] = v; }*/ bool ok(int y, int x) { if (y < 0 || y == h || x < 0 || x == w) return false; if (house[y][x] == '#') return false; return true; } int d1, d2; int c11, c12, c21, c22; int pushAdjs(int i, State& adj, State& curr, queue<State>& q, bool visA[16][16][16][16][16][16], bool visB[16][16][16][16][16][16], int da, int db, int& c) { if (i == n) { if (isVis(adj, visA)) return -1; if (isVis(adj, visB)) return da + 1 + db; setVis(adj, visA); q.push(adj); ++c; return -1; } for (int d = 0; d < 5; ++d) { adj.y[i] = dy[d] + curr.y[i]; adj.x[i] = dx[d] + curr.x[i]; if (!ok(adj.y[i], adj.x[i])) continue; bool okk = true; for (int j = 0; j < i; ++j) { if (adj.y[i] == curr.y[j] && adj.x[i] == curr.x[j]&& adj.y[j] == curr.y[i] && adj.x[j] == curr.x[i]) { okk = false; break; } if (adj.y[i] == adj.y[j] && adj.x[i] == adj.x[j]) { okk = false; break; } } if (!okk) continue; int cost = pushAdjs(i + 1, adj, curr, q, visA, visB, da, db, c); if (cost != -1) return cost; } return -1; } int meetMiddle(vector<int>& sy, vector<int>& sx, vector<int>& ty, vector<int>& tx) { memset(vis1, false, sizeof vis1); memset(vis2, false, sizeof vis2); State s(sy, sx); State t(ty, tx); //setDist(s, 0); //setDist(t, 0); d1 = 0; d2 = 0; c11 = 1, c12 = 0, c21 = 1, c22 = 0; setVis(s, vis1); setVis(t, vis2); queue<State> q1; queue<State> q2; q1.push(s); q2.push(t); while (true) { if(d1 <= d2) { State curr1 = q1.front(); q1.pop(); --c11; State adj; int cost = pushAdjs(0, adj, curr1, q1, vis1, vis2, d1, d2, c12); if (cost != -1) return cost; if(!c11){ swap(c11, c12); d1++; } } if(d2 < d1) { State curr2 = q2.front(); q2.pop(); --c21; State adj; int cost = pushAdjs(0, adj, curr2, q2, vis2, vis1, d2, d1, c22); if (cost != -1) return cost; if(!c21){ swap(c21, c22); d2++; } } } return -1; } int main() { //freopen("in.txt", "r", stdin); //clock_t begin = clock(); string line; while (cin >> w >> h >> n) { if (!w && !h && !n) break; cin.ignore(); //cin.ignore(); for (int i = 0; i < h; ++i) getline(cin, house[i]); vector<int> sx(3, 0); vector<int> sy(3, 0); vector<int> tx(3, 0); vector<int> ty(3, 0); for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (house[i][j] >= 'A' && house[i][j] <= 'C') { tx[(house[i][j] - 'A')] = j; ty[(house[i][j] - 'A')] = i; } if (house[i][j] >= 'a' && house[i][j] <= 'c') { sx[(house[i][j] - 'a')] = j; sy[(house[i][j] - 'a')] = i; } } } cout << meetMiddle(sy, sx, ty, tx) << endl; } }
#include <iostream> #include <array> #include <vector> #include <cctype> #include <unordered_set> #include <map> #include <queue> #include <algorithm> #include <cassert> #define repeat(i,n) for (int i = 0; (i) < (n); ++(i)) #define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i)) #define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i)) #define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(i)) #define dump(x) cerr << #x << " = " << (x) << endl #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl typedef long long ll; using namespace std; struct point_t { int y, x; }; point_t operator + (point_t const & a, point_t const & b) { return (point_t){ a.y + b.y, a.x + b.x }; } bool operator == (point_t const & a, point_t const & b) { return make_pair(a.y, a.x) == make_pair(b.y, b.x); } bool operator < (point_t const & a, point_t const & b) { return make_pair(a.y, a.x) < make_pair(b.y, b.x); } static point_t dp[5] = { {-1,0}, {1,0}, {0,1}, {0,-1}, {0,0} }; struct state_t { array<point_t,3> a; int cost; int dist; }; bool operator < (state_t const & a, state_t const & b) { return 1.3 * a.cost + a.dist > 1.3 * b.cost + b.dist; } uint32_t encode(array<point_t,3> const & s) { uint32_t q = 0; for (point_t p : s) { q = (q * 19) + p.y; q = (q * 16) + p.x; } return q; } bool is_valid_move(int i, int j, array<point_t,3> const & s, array<point_t,3> const & t) { if (t[i] == t[j]) return false; if (t[i] == s[j] and t[j] == s[i]) return false; return true; } int main() { while (true) { int w, h, n; cin >> w >> h >> n; cin.ignore(); if (w == 0 and h == 0 and n == 0) break; vector<string> c(h); repeat (i,h) getline(cin, c[i]); assert (1 <= n and n <= 3); array<point_t,3> start, goal; repeat (y,h) repeat (x,w) { if (islower(c[y][x])) { start[c[y][x] - 'a'] = { y, x }; } else if (isupper(c[y][x])) { goal[c[y][x] - 'A'] = { y, x }; } } if (n == 1 or n == 2) { assert (4 <= w); string w = c.back(); c.push_back(w); // c c.back()[2] = '.'; start[2] = goal[2] = (point_t){ h, 2 }; if (n == 1) { // b c.back()[1] = '.'; start[1] = goal[1] = (point_t){ h, 1 }; } c.push_back(w); h += 2; } vector<vector<int> > dist[3]; // from goal repeat (i,3) { dist[i].resize(h, vector<int>(w, 1000000007)); // bfs vector<point_t> cur, nxt; nxt.push_back(goal[i]); dist[i][goal[i].y][goal[i].x] = 0; int l = 1; while (not nxt.empty()) { cur.clear(); cur.swap(nxt); for (auto p : cur) { repeat (j,4) { auto q = p + dp[j]; if (c[q.y][q.x] == '#') continue; if (l < dist[i][q.y][q.x]) { dist[i][q.y][q.x] = l; nxt.push_back(q); } } } ++ l; } } map<point_t,int> ix; repeat (y,h) repeat (x,w) { if (c[y][x] != '#') { int i = ix.size(); ix[(point_t){ y, x }] = i; } } vector<vector<vector<bool> > > used(ix.size(), vector<vector<bool> >(ix.size(), vector<bool>(ix.size()))); priority_queue<state_t> que; { state_t initial = { start, 0, 0 }; repeat (i,3) initial.dist += dist[i][start[i].y][start[i].x]; used[ix[start[0]]][ix[start[1]]][ix[start[2]]] = true; que.push(initial); } while (not que.empty()) { state_t st = que.top(); que.pop(); array<point_t,3> & s = st.a; if (s == goal) { cout << st.cost << endl; break; } state_t tt = st; tt.cost += 1; array<point_t,3> & t = tt.a; repeat (i,5) { // a t[0] = s[0] + dp[i]; if (c[t[0].y][t[0].x] == '#') continue; repeat (j,5) { // b t[1] = s[1] + dp[j]; if (c[t[1].y][t[1].x] == '#') continue; if (not is_valid_move(0, 1, s, t)) continue; repeat (k,5) { // c t[2] = s[2] + dp[k]; if (c[t[2].y][t[2].x] == '#') continue; if (not is_valid_move(1, 2, s, t)) continue; if (not is_valid_move(2, 0, s, t)) continue; if (used[ix[t[0]]][ix[t[1]]][ix[t[2]]]) continue; used[ix[t[0]]][ix[t[1]]][ix[t[2]]] = true; tt.dist = 0; repeat (i,3) tt.dist = max(tt.dist, dist[i][t[i].y][t[i].x]); que.push(tt); } } } } } return 0; }
#include <cstdio> #include <cstring> #include <queue> #include <cctype> using namespace std; unsigned char vis[1 << 24]; char c[16][20]; bool wall[256]; int gen; inline bool check(int u, int v){ for(int i = 0; i < 24; i += 8){ int p1 = v >> i & 255; if(p1){ int j = i == 16 ? 0 : i + 8; if(wall[p1]){ return false; } int p2 = v >> j & 255; if(p1 == p2){ return false; } if( p1 == (u >> j & 255) && p2 == (u >> i & 255) ){ return false; } } } return true; } int solve(int start, int goal){ const int dif[5] = {0, -1, 1, -16, 16}; ++gen; queue<int> q; q.push(start); q.push(-1); int tm = 1; vis[start] = gen; while(1){ int u = q.front(); q.pop(); if(u < 0){ q.push(-1); ++tm; } else{ for(int i1 = u & 255 ? 4 : 0; i1 >= 0; --i1) for(int i2 = u >> 8 & 255 ? 4 : 0; i2 >= 0; --i2) for(int i3 = u >> 16 ? 4 : 0; i3 >= 0; --i3){ int v = u + dif[i1] + (dif[i2] << 8) + (dif[i3] << 16); if(vis[v] == gen){ continue; } if(check(u, v)){ if(v == goal){ return tm; } vis[v] = gen; q.push(v); } } } } } int main(){ int w, h; while(scanf("%d%d%*d ", &w, &h), w){ for(int i = 0; i < h; ++i){ fgets(c[i], 20, stdin); } int goal = 0; int start = 0; for(int i = 0; i < h; ++i){ for(int j = 0; j < w; ++j){ if(c[i][j] == '#'){ wall[i << 4 | j] = true; } else{ wall[i << 4 | j] = false; if(isupper(c[i][j])){ goal |= (i << 4 | j) << (c[i][j] - 'A') * 8; } else if(islower(c[i][j])){ start |= (i << 4 | j) << (c[i][j] - 'a') * 8; } } } } printf("%d\n", solve(start, goal)); if(gen == 255){ memset(vis, 0, sizeof vis); gen = 0; } } }
#include "bits/stdc++.h" using namespace std; static const int dy[] = { -1, 0, 1, 0, 0}, dx[] = { 0, -1, 0, 1, 0 }; struct state { int a, b, c, step; }; bool used[260][260][260]; int main() { int w, h, n; while (true) { cin >> w >> h >> n; cin.ignore(); if (n == 0) break; vector<string> m(h); for (int i = 0; i < h; i ++) getline(cin, m[i]); int as, ag, bs = (h - 1) * w + (w - 1), bg = (h - 1) * w + (w - 1), cs = 0, cg = 0; for (int i = 0; i < h; i ++) { for (int j = 0; j < w; j ++) { if (m[i][j] == 'A') as = i * w + j; if (m[i][j] == 'a') ag = i * w + j; if (m[i][j] == 'B') bs = i * w + j; if (m[i][j] == 'b') bg = i * w + j; if (m[i][j] == 'C') cs = i * w + j; if (m[i][j] == 'c') cg = i * w + j; } } queue<state> q; memset(used, false, sizeof(used)); used[as][bs][cs] = true; q.push((state) { as, bs, cs }); int ans = -1; while (!q.empty()) { state now = q.front(); q.pop(); if (now.a == ag && now.b == bg && now.c == cg) { ans = now.step; break; } int noway = now.a / w, nowax = now.a % w; int nowby = now.b / w, nowbx = now.b % w; int nowcy = now.c / w, nowcx = now.c % w; for (int da = 0; da < 5; da ++) { int neway = noway + dy[da], newax = nowax + dx[da]; if (neway < 0 || h <= neway || newax < 0 || w <= newax) continue; if (m[neway][newax] == '#') continue; for (int db = 0; db < 5; db ++) { int newby = nowby + dy[db], newbx = nowbx + dx[db]; if (newby < 0 || h <= newby || newbx < 0 || w <= newbx) continue; if (n > 1) if (m[newby][newbx] == '#') continue; for (int dc = 0; dc < 5; dc ++) { int newcy = nowcy + dy[dc], newcx = nowcx + dx[dc]; if (newcy < 0 || h <= newcy || newcx < 0 || w <= newcx) continue; if (n > 2) if (m[newcy][newcx] == '#') continue; int newa = neway * w + newax; int newb = newby * w + newbx; int newc = newcy * w + newcx; if (newa == newb || newa == newc || newb == newc) continue; //Rule 1. if (newa == now.b && newb == now.a) continue; //Rule 2. if (newa == now.c && newc == now.a) continue; // if (newb == now.c && newc == now.b) continue; // if (used[newa][newb][newc]) continue; used[newa][newb][newc] = true; q.push((state) { newa, newb, newc, now.step + 1 }); } } } } cout << ans << endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <tuple> using namespace std; const int inf = 1e9; const int dx[5] = {1, 0, -1, 0, 0}; const int dy[5] = {0, 1, 0, -1, 0}; int mindist[256][256][256]; void init(){ for(int i=0; i<256; i++){ for(int j=0; j<256; j++){ for(int k=0; k<256; k++){ mindist[i][j][k] = inf; } } } } struct info{ int p[3]; int dist; info(int a, int b, int c, int d):p{a,b,c},dist(d){} info(){} }; int main(){ while(1){ int w,h,n; cin >> w >> h >> n; if(w == 0) break; //input bool wall[16][16] = {}; vector<pair<int, int> > s, g; cin.ignore(); for(int i=0; i<h; i++){ string str; getline(cin, str); for(int j=0; j<w; j++){ if('a'<=str[j] && str[j]<='z'){ s.emplace_back(str[j], (i<<4) +j); } if('A'<=str[j] && str[j]<='Z'){ g.emplace_back(str[j], (i<<4) +j); } if(str[j] == '#') wall[i][j] = true; } } while((int)s.size() < 3){ s.emplace_back(0, s.size()); g.emplace_back(0, g.size()); } sort(s.begin(), s.end()); sort(g.begin(), g.end()); //bfs init(); mindist[s[0].second][s[1].second][s[2].second] = 0; queue<info> wait; info start(s[0].second, s[1].second, s[2].second, 0); info goal(g[0].second, g[1].second, g[2].second, 0); wait.push(start); while(!wait.empty()){ info curr = wait.front(); wait.pop(); int p[3] = {curr.p[0], curr.p[1], curr.p[2]}; int py[3] = {p[0]>>4, p[1]>>4, p[2]>>4}; int px[3] = {p[0]&0b1111, p[1]&0b1111, p[2]&0b1111}; int dist = curr.dist; if(p[0]==goal.p[0] && p[1]==goal.p[1] && p[2]==goal.p[2]) break; int np[3]; int y,x; for(int i=0; i<5; i++){ if(py[0]==0 && i<4) continue; y = py[0] +dy[i]; x = px[0] +dx[i]; if(py[0]!=0 && wall[y][x]) continue; np[0] = y*16 +x; for(int j=0; j<5; j++){ if(py[1]==0 && j<4) continue; y = py[1] +dy[j]; x = px[1] +dx[j]; if(py[1]!=0 && wall[y][x]) continue; np[1] = y*16 +x; if(np[0] == np[1] || (np[0]==p[1] && np[1]==p[0])) continue; for(int k=0; k<5; k++){ y = py[2] +dy[k]; x = px[2] +dx[k]; if(wall[y][x]) continue; np[2] = y*16 +x; if(np[1] == np[2] || (np[1]==p[2] && np[2]==p[1])) continue; if(np[2] == np[0] || (np[2]==p[0] && np[0]==p[2])) continue; if(dist +1 < mindist[np[0]][np[1]][np[2]]){ mindist[np[0]][np[1]][np[2]] = dist +1; wait.push(info(np[0], np[1], np[2], dist +1)); } } } } } cout << mindist[g[0].second][g[1].second][g[2].second] << endl; } return 0; }
#include<bits/stdc++.h> #define INF (1e9) #define N 14 #define rep(i,n) for(int i=0;i<n;i++) using namespace std; struct dat{ int cost, ay, ax, by, bx, cy, cx; }; int w, h, n; int d[N][N][N][N][N][N]; string s[16]; int ay, ax, by, bx, cy, cx; int dy[5]={-1,0,1,0,0}; int dx[5]={0,1,0,-1,0}; queue<dat> q; bool check(int nay, int nax){ if(nay<0||nax<0||h<=nay||w<=nax) return false; if(s[nay][nax]=='#') return false; return true; } bool check2(int Ay,int Ax,int By,int Bx,int ai,int bi){ if(Ay==By){ if(Ax+1==Bx&&ai==1&&bi==3) return false; if(Bx+1==Ax&&bi==1&&ai==3) return false; } if(Ax==Bx){ if(Ay+1==By&&ai==2&&bi==0) return false; if(By+1==Ay&&bi==2&&ai==0) return false; } return true; } int bnf(){ rep(i,N) rep(j,N) rep(k,N) rep(l,N) rep(m,N) rep(o,N) d[i][j][k][l][m][o]=INF; d[ay][ax][by][bx][cy][cx]=0; q.push(dat{0,ay,ax,by,bx,cy,cx}); int res; while(!q.empty()){ dat t=q.front(); q.pop(); if(n==1&&s[t.ay][t.ax]=='A'+s[ay][ax]-'a'){ res=t.cost; break; } if(n==2&&s[t.ay][t.ax]=='A'+s[ay][ax]-'a'&& s[t.by][t.bx]=='A'+s[by][bx]-'a'){ res=t.cost; break; } if(n==3&&s[t.ay][t.ax]=='A'+s[ay][ax]-'a'&& s[t.by][t.bx]=='A'+s[by][bx]-'a'&& s[t.cy][t.cx]=='A'+s[cy][cx]-'a'){ res=t.cost; break; } rep(i,5){ int nay=dy[i]+t.ay, nax=dx[i]+t.ax; if(!check(nay,nax)) continue; rep(j,5){ int nby=dy[j]+t.by, nbx=dx[j]+t.bx; if(n==1) nby=0, nbx=0; if(n>=2&&!check(nby,nbx)) continue; rep(k,5){ int ncy=dy[k]+t.cy, ncx=dx[k]+t.cx; if(n<=2) ncy=0, ncx=0; if(n==3&&!check(ncy,ncx)) continue; if(n>=2&&!check2(t.ay,t.ax,t.by,t.bx,i,j)) continue; if(n==3&&!check2(t.ay,t.ax,t.cy,t.cx,i,k)) continue; if(n==3&&!check2(t.by,t.bx,t.cy,t.cx,j,k)) continue; if(n>=2&&nay==nby&&nax==nbx) continue; if(n==3&&nay==ncy&&nax==ncx) continue; if(n==3&&nby==ncy&&nbx==ncx) continue; if(d[nay][nax][nby][nbx][ncy][ncx]>d[t.ay][t.ax][t.by][t.bx][t.cy][t.cx]+1){ d[nay][nax][nby][nbx][ncy][ncx]=d[t.ay][t.ax][t.by][t.bx][t.cy][t.cx]+1; q.push(dat{d[nay][nax][nby][nbx][ncy][ncx],nay,nax,nby,nbx,ncy,ncx}); } } } } } return res; } int main(){ while(1){ cin>>w>>h>>n; if(!w&&!h&&!n) break; unordered_set<char> memo; ay=ax=by=bx=cy=cx=0; getline(cin,s[0]); rep(i,h) getline(cin,s[i]); for(int i=0;i<h-2;i++){ s[i]=s[i+1]; s[i]=s[i].substr(1,w-2); rep(j,w-2){ char c=s[i][j]; if('a'<=c&&c<='z'&&!memo.count(c)){ if(memo.size()==0) ay=i, ax=j; if(memo.size()==1) by=i, bx=j; if(memo.size()==2) cy=i, cx=j; memo.insert(c); } } } h-=2; w-=2; cout<<bnf()<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} using pi = pair<int,int>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,-1,0,1}; struct State{ int pa,pb,pc; }; const int N = 16*16; const int INF = 19191919; int dp[N][N][N]; int main(){ int w,h,n; while(cin >>w >>h >>n,w){ cin.ignore(); vector<string> f(h); rep(i,h) getline(cin, f[i]); auto cv = [&](pi p){ return p.fi*w+p.se; }; int sa=0,sb=0,sc=0,ga=0,gb=0,gc=0; rep(i,h)rep(j,w){ if(f[i][j]=='a') sa = cv({i,j}); if(f[i][j]=='b') sb = cv({i,j}); if(f[i][j]=='c') sc = cv({i,j}); if(f[i][j]=='A') ga = cv({i,j}); if(f[i][j]=='B') gb = cv({i,j}); if(f[i][j]=='C') gc = cv({i,j}); } auto cand = [&](int p){ int y = p/w, x = p%w; vector<int> ret; ret.pb(p); rep(d,4){ int ny = y+dy[d], nx = x+dx[d]; if(0<=ny && ny<h && 0<=nx && nx<w && f[ny][nx]!='#') ret.pb(cv({ny,nx})); } return ret; }; rep(i,N)rep(j,N)rep(k,N) dp[i][j][k] = INF; dp[sa][sb][sc] = 0; queue<State> que; que.push({sa,sb,sc}); while(!que.empty()){ State now = que.front(); que.pop(); vector<int> da = cand(now.pa), db = cand(now.pb), dc = cand(now.pc); for(int A:da)for(int B:db)for(int C:dc){ if(B != 0){ if(B == A) continue; if(now.pa == B && now.pb == A) continue; } if(C != 0){ if(C == A || C == B) continue; if(now.pa == C && now.pc == A) continue; if(now.pb == C && now.pc == B) continue; } if(dp[A][B][C] > dp[now.pa][now.pb][now.pc]+1){ dp[A][B][C] = dp[now.pa][now.pb][now.pc]+1; que.push({A,B,C}); } } } cout << dp[ga][gb][gc] << endl; } return 0; }
#include <cstdio> #include <map> #include <queue> #include <algorithm> using namespace std; static const int di[] = {0, -1, 1, 0, 0}, dj[] = {0, 0, 0, -1, 1}; inline int h(int s, const unsigned short hh[3][256], int N) { unsigned short l = 0; for (int i = 0; i < N; i++) { l = max(l, hh[i][(s>>(i<<3))&0xff]); } return l; } int calc_next(int N, const char grid[16][20], int s, int *a) { int next = 0; for (int i = 0; i < N; i++) { const int t = (s>>(i<<3))&0xff; const int k = (t>>4) + di[a[i]]; const int l = (t&0xf) + dj[a[i]]; if (grid[k][l] == '#') { return -i-1; } const int u = (k<<4)|l; const int v = (s>>(i<<3))&0xff; for (int j = 0; j < i; j++) { const int w = (next>>(j<<3))&0xff; if (u == w) { return 0; } if (u == ((s>>(j<<3))&0xff) && w == v) { return 0; } } next |= u<<(i<<3); } return next; } inline int get(int s, const unsigned char *d1, const map<int,short>& d2) { int d = d1[s]; if (d == 255) { return 1000; } else if (d == 254) { return d2.find(s)->second; } else { return d; } } inline void set(int s, unsigned char *d1, map<int,short>& d2, int d) { if (d >= 254) { d1[s] = 254; d2[s] = d; } else { d1[s] = d; } } int main() { char buf[100]; while (fgets(buf, sizeof buf, stdin)) { int W, H, N; sscanf(buf, "%d %d %d", &W, &H, &N); if (N == 0) { break; } char grid[16][20]; int init = 0, final = 0; for (int i = 0; i < H; i++) { fgets(grid[i], 20, stdin); for (int j = 0; j < W; j++) { if ('a' <= grid[i][j] && grid[i][j] <= 'c') { init |= (i<<4|j) << ((grid[i][j]-'a')<<3); grid[i][j] = ' '; } else if ('A' <= grid[i][j] && grid[i][j] <= 'C') { final |= (i<<4|j) << ((grid[i][j]-'A')<<3); grid[i][j] = ' '; } } } const int A = N >= 0 ? 5 : 1; const int B = N >= 1 ? 5 : 1; const int C = N >= 2 ? 5 : 1; static unsigned short int hh[3][256]; for (int i = 0; i < N; i++) { fill_n(hh[i], 256, 1000); queue<int> q; q.push((final>>(i<<3))&0xff); hh[i][(final>>(i<<3))&0xff] = 0; while (!q.empty()) { const int s = q.front(); const int x = s>>4; const int y = s&0xf; q.pop(); for (int d = 1; d < 5; d++) { const int k = x + di[d]; const int l = y + dj[d]; const int t = (k<<4)|l; if (grid[k][l] != '#' && hh[i][s]+1 < hh[i][t]) { hh[i][t] = hh[i][s]+1; q.push(t); } } } } static unsigned char dist[256*256*256]; fill_n(dist, 256*256*256, 255); map<int,short> dist2; dist[init] = 0; priority_queue<pair<short,int> > q; q.push(make_pair(-h(init, hh, N), init)); while (!q.empty()) { const int cost = -q.top().first; const int s = q.top().second; q.pop(); if (s == final) { printf("%d\n", cost); break; } const int d = cost - h(s, hh, N); const int d2 = get(s, dist, dist2); if (d > d2) { continue; } bool inv[5] = {false, false, false, false, false}; int a[3]; for (a[0] = 0; a[0] < A; a[0]++) { for (a[1] = 0; a[1] < B; a[1]++) { if (inv[a[1]]) { continue; } for (a[2] = 0; a[2] < C; a[2]++) { const int next = calc_next(N, grid, s, a); if (next == 0) { continue; } else if (next < 0) { if (next == -1) { goto SKIP_A; } else if (next == -2) { inv[a[1]] = true; goto SKIP_B; } else { continue; } } const int du = get(next, dist, dist2); if (d+1 < du) { set(next, dist, dist2, d+1); q.push(make_pair(-d-1-h(next, hh, N), next)); } } SKIP_B: ; } SKIP_A: ; } } } return 0; }
#include "bits/stdc++.h" using namespace std; #define rep(i, n) for (int i = 0; i < (n); i ++) static const int dy[] = { -1, 0, 1, 0, 0}, dx[] = { 0, -1, 0, 1, 0 }; struct state { int a; int b; int c; int step; }; bool used[260][260][260]; int main() { int w, h, n; while (true) { cin >> w >> h >> n; cin.ignore(); if (n == 0) break; vector<string> m(h); for (int i = 0; i < h; i ++) { getline(cin, m[i]); } int as, ag, bs = (h - 1) * w + (w - 1), bg = (h - 1) * w + (w - 1), cs = 0, cg = 0; for (int i = 0; i < h; i ++) { for (int j = 0; j < w; j ++) { if (m[i][j] == 'A') as = i * w + j; if (m[i][j] == 'a') ag = i * w + j; if (m[i][j] == 'B') bs = i * w + j; if (m[i][j] == 'b') bg = i * w + j; if (m[i][j] == 'C') cs = i * w + j; if (m[i][j] == 'c') cg = i * w + j; } } queue<state> q; memset(used, false, sizeof(used)); used[as][bs][cs] = true; q.push((state) { as, bs, cs }); int ans = -1; while (!q.empty()) { state now = q.front(); q.pop(); if (now.a == ag && now.b == bg && now.c == cg) { ans = now.step; break; } int noway = now.a / w, nowax = now.a % w; int nowby = now.b / w, nowbx = now.b % w; int nowcy = now.c / w, nowcx = now.c % w; for (int da = 0; da < 5; da ++) { int neway = noway + dy[da], newax = nowax + dx[da]; if (neway < 0 || h <= neway || newax < 0 || w <= newax) continue; if (m[neway][newax] == '#') continue; for (int db = 0; db < 5; db ++) { int newby = nowby + dy[db], newbx = nowbx + dx[db]; if (newby < 0 || h <= newby || newbx < 0 || w <= newbx) continue; if (n > 1) if (m[newby][newbx] == '#') continue; for (int dc = 0; dc < 5; dc ++) { int newcy = nowcy + dy[dc], newcx = nowcx + dx[dc]; if (newcy < 0 || h <= newcy || newcx < 0 || w <= newcx) continue; if (n > 2) if (m[newcy][newcx] == '#') continue; int newa = neway * w + newax; int newb = newby * w + newbx; int newc = newcy * w + newcx; if (used[newa][newb][newc]) continue; if (newa == newb || newa == newc || newb == newc) continue; if (newa == now.b && newb == now.a) continue; if (newa == now.c && newc == now.a) continue; if (newb == now.c && newc == now.b) continue; used[newa][newb][newc] = true; q.push((state) { newa, newb, newc, now.step + 1 }); } } } } cout << ans << endl; } return 0; }
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <math.h> #include <assert.h> #include <vector> #include <queue> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) int width, height, n; int encode(const int x[3], const int y[3]) { int ret = 0; int base = 1; REP(i, n) { assert(x[i] > 0 && x[i] < width - 1 && y[i] > 0 && y[i] < height - 1); ret += x[i] * base; base *= 16; ret += y[i] * base; base *= 16; } assert(ret > 0 && ret < 16 * 16 * 16 * 16 * 16 * 16); return ret; } void decode(int code, int x[3], int y[3]) { REP(i, n) { x[i] = code % 16; code /= 16; y[i] = code % 16; code /= 16; } } char field[20][20]; int sx[3]; int sy[3]; int gx[3]; int gy[3]; int x[3]; int y[3]; int nx[3]; int ny[3]; bool visit[16*16*16*16*16*16]; const int dx[5] = { 1, 0, -1, 0, 0 }; const int dy[5] = { 0, 1, 0, -1, 0 }; int main() { while (scanf("%d %d %d", &width, &height, &n), width|height|n) { fgets(field[0], 19, stdin); MEMSET(visit, false); MEMSET(field, '#'); { REP(y, height) { fgets(field[y], 19, stdin); REP(x, width) { if (field[y][x] == 'A') { gx[0] = x; gy[0] = y; } if (field[y][x] == 'B') { gx[1] = x; gy[1] = y; } if (field[y][x] == 'C') { gx[2] = x; gy[2] = y; } if (field[y][x] == 'a') { sx[0] = x; sy[0] = y; } if (field[y][x] == 'b') { sx[1] = x; sy[1] = y; } if (field[y][x] == 'c') { sx[2] = x; sy[2] = y; } } //cout << field[y]; } } queue<pair<int, int> > que; que.push(make_pair(encode(sx, sy), 0)); visit[encode(sx, sy)] = true; while (!que.empty()) { decode(que.front().first, x, y); int cost = que.front().second; que.pop(); //cout << x[0] << " " << y[0] << " " << x[1] << " " << y[1] << " " << cost << endl; REP(i, n) { if (x[i] != gx[i] || y[i] != gy[i]) { break; } if (i == n - 1) { printf("%d\n", cost); goto next; } } REP(dir1, 5) { nx[0] = x[0] + dx[dir1]; ny[0] = y[0] + dy[dir1]; if (n >= 1 && field[ny[0]][nx[0]] == '#') { continue; } REP(dir2, 5) { if (n < 2 && dir2 != 4) { continue; } nx[1] = x[1] + dx[dir2]; ny[1] = y[1] + dy[dir2]; if (n >= 2) { if (nx[0] == nx[1] && ny[0] == ny[1]) { continue; } if (x[0] == nx[1] && x[1] == nx[0] && y[0] == ny[1] && y[1] == ny[0]) { continue; } } if (n >= 2 && field[ny[1]][nx[1]] == '#') { continue; } REP(dir3, 5) { if (n < 3 && dir3 != 4) { continue; } nx[2] = x[2] + dx[dir3]; ny[2] = y[2] + dy[dir3]; if (n >= 3 && field[ny[2]][nx[2]] == '#') { continue; } if (n >= 3) { if (nx[0] == nx[2] && ny[0] == ny[2]) { continue; } if (nx[1] == nx[2] && ny[1] == ny[2]) { continue; } if (x[0] == nx[2] && x[2] == nx[0] && y[0] == ny[2] && y[2] == ny[0]) { continue; } if (x[1] == nx[2] && x[2] == nx[1] && y[1] == ny[2] && y[2] == ny[1]) { continue; } } int code = encode(nx, ny); if (visit[code]) { continue; } visit[code] = true; que.push(make_pair(code, cost + 1)); } } } } next:; } }
#include<stdio.h> #include<algorithm> #include<queue> #include<vector> using namespace std; char str[20][20]; int dx[]={1,0,-1,0,0}; int dy[]={0,1,0,-1,0}; int num[20][20]; int bfs[130][130][130]; int to[130][5]; int cnt; struct wolf{ int a,b,c; wolf(int A,int B,int C){ a=A;b=B;c=C; } wolf(){ a=b=c=cnt; } }; int main(){ int a,b,c; while(scanf("%d%d%d",&b,&a,&c),a){ gets(str[0]); for(int i=0;i<a;i++)gets(str[i]); for(int i=0;i<a;i++)for(int j=0;j<b;j++)num[i][j]=-1; int cnt=0; for(int i=0;i<a;i++){ for(int j=0;j<b;j++)if(str[i][j]!='#')num[i][j]=cnt++; } for(int i=0;i<a;i++)for(int j=0;j<b;j++)if(str[i][j]!='#'){ for(int k=0;k<5;k++){ if(0<=i+dx[k]&&i+dx[k]<a&&0<=j+dy[k]&&j+dy[k]<b&&str[i+dx[k]][j+dy[k]]!='#'){ to[num[i][j]][k]=num[i+dx[k]][j+dy[k]]; }else to[num[i][j]][k]=-1; } } queue<wolf>Q; wolf S; wolf T; for(int i=0;i<a;i++)for(int j=0;j<b;j++){ if(str[i][j]=='a'){S.a=num[i][j];str[i][j]='.';} if(str[i][j]=='b'){S.b=num[i][j];str[i][j]='.';} if(str[i][j]=='c'){S.c=num[i][j];str[i][j]='.';} if(str[i][j]=='A'){T.a=num[i][j];str[i][j]='.';} if(str[i][j]=='B'){T.b=num[i][j];str[i][j]='.';} if(str[i][j]=='C'){T.c=num[i][j];str[i][j]='.';} } for(int i=0;i<=cnt;i++)for(int j=0;j<=cnt;j++)for(int k=0;k<=cnt;k++) bfs[i][j][k]=-1; Q.push(S); bfs[S.a][S.b][S.c]=0; while(Q.size()&&!~bfs[T.a][T.b][T.c]){ wolf at=Q.front();Q.pop(); for(int i=0;i<5;i++){ if(!~to[at.a][i])continue; if(c==1){ if(!~bfs[to[at.a][i]][at.b][at.c]){ bfs[to[at.a][i]][at.b][at.c]=bfs[at.a][at.b][at.c]+1; Q.push(wolf(to[at.a][i],at.b,at.c)); } continue; } for(int j=0;j<5;j++){ if(!~to[at.b][j])continue; if(c==2){ int ta=to[at.a][i]; int tb=to[at.b][j]; if(!~bfs[ta][tb][at.c]&&ta!=tb&&(ta!=at.b||tb!=at.a)){ bfs[ta][tb][at.c]=bfs[at.a][at.b][at.c]+1; Q.push(wolf(ta,tb,at.c)); } continue; } for(int k=0;k<5;k++){ if(!~to[at.c][k])continue; int ta=to[at.a][i]; int tb=to[at.b][j]; int tc=to[at.c][k]; if(!~bfs[ta][tb][tc]&&ta!=tb&&ta!=tc&&tb!=tc&&(ta!=at.b||tb!=at.a)&& (tb!=at.c||tc!=at.b)&&(tc!=at.a||ta!=at.c)){ bfs[ta][tb][tc]=bfs[at.a][at.b][at.c]+1; Q.push(wolf(ta,tb,tc)); } } } } } printf("%d\n",bfs[T.a][T.b][T.c]); } }
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<vector> #include<utility> #include<map> #define MAX_W 16 + 5 #define MAX_N 3 + 2 #define INF 0x3f3f3f3f using namespace std; typedef pair<int, int> P; int w, h, n; char m[MAX_W][MAX_W]; int d[MAX_N][MAX_W][MAX_W]; int dir[MAX_N]; map<int, int>::iterator it; int x[] = {0, 1, -1, 0, 0}; //不動/下/上/右/左 int y[] = {0, 0, 0, 1, -1}; int ct = 0; int hstar(int cur){ int res = 0; for(int i = 0; i < n; i++){ int sx = (cur >> 4) & 0xf; int sy = cur & 0xf; cur >>= 8; res = max(res, d[i][sx][sy]); } return res; } int check(int cur, int *dir){ int sxy[3], nxy[3], nxt = 0; for(int i = 0; i < 3; i++){ sxy[i] = (cur >> (i * 8)) & 0xff; int nx = ((sxy[i] >> 4) & 0xf) + x[dir[i]]; int ny = (sxy[i] & 0xf) + y[dir[i]]; nxy[i] = (nx << 4) | ny; } for(int i = 0; i < n; i++){ int nx = (nxy[i] >> 4) & 0xf; int ny = nxy[i] & 0xf; if(m[nx][ny] == '#') return 0; //hit the wall for(int j = i + 1; j < n; j++){ if((nxy[i] == nxy[j]) || (nxy[i] == sxy[j] && sxy[i] == nxy[j])) return 0; // overlap && switch } nxt |= (nxy[i] << (i * 8)); } return nxt; } // void show(int cur){ // for(int i = 0; i < n; i++){ // int sx = cur >> 4 & 0xf; // int sy = cur & 0xf; // cur >>= 8; // printf("%c : (%d, %d)\n", i + 'a', sx, sy); // } // } int solve(){ memset(d, 0x3f, sizeof(d)); //BFS for(int i = 0; i < n; i++){ int sx, sy; for(int j = 0; j < w * h; j++){ if(m[j / w][j % w] == i + 'A'){ sx = j / w, sy = j % w; break; } } queue<P> que; que.push(make_pair(sx, sy)); d[i][sx][sy] = 0; while(!que.empty()){ P p = que.front(); que.pop(); for(int j = 1; j < 5; j++){ int nx = p.first + x[j]; int ny = p.second + y[j]; if(m[nx][ny] != '#' && d[i][nx][ny] > d[i][p.first][p.second] + 1){ d[i][nx][ny] = d[i][p.first][p.second] + 1; que.push(make_pair(nx, ny)); } } } } int cur = 0, end = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < w * h; j++){ if(m[j / w][j % w] == i + 'a'){ cur |= ((j / w) << (8 * i + 4)); cur |= ((j % w) << (8 * i)); } if(m[j / w][j % w] == i + 'A'){ end |= ((j / w) << (8 * i + 4)); end |= ((j % w) << (8 * i)); } } } priority_queue<P, vector<P>, greater<P> > que; map<int, int> mp; mp[cur] = 0; que.push(make_pair(hstar(cur), cur)); while(!que.empty()){ cur = que.top().second; que.pop(); if(cur == end) return mp[cur]; ct++; for(int i = 0; i < 5; i++){ dir[0] = i; for(int j = 0; j < (n < 2 ? 1 : 5); j++){ dir[1] = j; for(int k = 0; k < (n < 3 ? 1 : 5); k++){ dir[2] = k; int nx = check(cur, dir); if(nx > 0){ //確認下一步是合法的 if(mp.find(nx) == mp.end() || mp[nx] > mp[cur] + 1){ mp[nx] = mp[cur] + 1; que.push(make_pair(mp[nx] + hstar(nx), nx)); //d(v) + h*(v) } } } } } } return -1; } int main(){ while(scanf("%d %d %d\n", &w, &h, &n) != EOF && (w || h || n)){ for(int i = 0; i < h; i++){ scanf("%[^\n]%*c", m[i]); } ct = 0; cout << solve() << endl; //printf("ct = %d\n", ct); } return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<unordered_map> #include<vector> #include<utility> #include<map> #define MAX_W 16 + 5 #define MAX_N 3 + 2 #define INF 0x3f3f3f3f using namespace std; typedef pair<int, int> P; int w, h, n; char m[MAX_W][MAX_W]; int d[MAX_N][MAX_W][MAX_W]; int dir[MAX_N]; map<int, int>::iterator it; int x[] = {0, 1, -1, 0, 0}; //不動/下/上/右/左 int y[] = {0, 0, 0, 1, -1}; int ct = 0; int hstar(int cur){ int res = 0; for(int i = 0; i < n; i++){ int sx = (cur >> 4) & 0xf; int sy = cur & 0xf; cur >>= 8; res = max(res, d[i][sx][sy]); } return res; } int check(int cur, int *dir){ int sxy[3], nxy[3], nxt = 0; for(int i = 0; i < 3; i++){ sxy[i] = (cur >> (i * 8)) & 0xff; int nx = ((sxy[i] >> 4) & 0xf) + x[dir[i]]; int ny = (sxy[i] & 0xf) + y[dir[i]]; nxy[i] = (nx << 4) | ny; } for(int i = 0; i < n; i++){ int nx = (nxy[i] >> 4) & 0xf; int ny = nxy[i] & 0xf; if(m[nx][ny] == '#') return 0; //hit the wall for(int j = i + 1; j < n; j++){ if((nxy[i] == nxy[j]) || (nxy[i] == sxy[j] && sxy[i] == nxy[j])) return 0; // overlap && switch } nxt |= (nxy[i] << (i * 8)); } return nxt; } // void show(int cur){ // for(int i = 0; i < n; i++){ // int sx = cur >> 4 & 0xf; // int sy = cur & 0xf; // cur >>= 8; // printf("%c : (%d, %d)\n", i + 'a', sx, sy); // } // } int solve(){ memset(d, 0x3f, sizeof(d)); //BFS for(int i = 0; i < n; i++){ int sx, sy; for(int j = 0; j < w * h; j++){ if(m[j / w][j % w] == i + 'A'){ sx = j / w, sy = j % w; break; } } queue<P> que; que.push(make_pair(sx, sy)); d[i][sx][sy] = 0; while(!que.empty()){ P p = que.front(); que.pop(); for(int j = 1; j < 5; j++){ int nx = p.first + x[j]; int ny = p.second + y[j]; if(m[nx][ny] != '#' && d[i][nx][ny] > d[i][p.first][p.second] + 1){ d[i][nx][ny] = d[i][p.first][p.second] + 1; que.push(make_pair(nx, ny)); } } } } int cur = 0, end = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < w * h; j++){ if(m[j / w][j % w] == i + 'a'){ cur |= ((j / w) << (8 * i + 4)); cur |= ((j % w) << (8 * i)); } if(m[j / w][j % w] == i + 'A'){ end |= ((j / w) << (8 * i + 4)); end |= ((j % w) << (8 * i)); } } } priority_queue<P, vector<P>, greater<P> > que; unordered_map<int, int> mp; mp[cur] = 0; que.push(make_pair(hstar(cur), cur)); while(!que.empty()){ cur = que.top().second; que.pop(); if(cur == end) return mp[cur]; ct++; for(int i = 0; i < 5; i++){ dir[0] = i; for(int j = 0; j < (n < 2 ? 1 : 5); j++){ dir[1] = j; for(int k = 0; k < (n < 3 ? 1 : 5); k++){ dir[2] = k; int nx = check(cur, dir); if(nx > 0){ //確認下一步是合法的 if(mp.find(nx) == mp.end() || mp[nx] > mp[cur] + 1){ mp[nx] = mp[cur] + 1; que.push(make_pair(mp[nx] + hstar(nx), nx)); //d(v) + h*(v) } } } } } } return -1; } int main(){ while(scanf("%d %d %d\n", &w, &h, &n) != EOF && (w || h || n)){ for(int i = 0; i < h; i++){ scanf("%[^\n]%*c", m[i]); } ct = 0; cout << solve() << endl; //printf("ct = %d\n", ct); } return 0; }
#include<bits/stdc++.h> using namespace std; int dy[5]={0,-1,0,1,0}; int dx[5]={0,0,1,0,-1}; int H,W,N; string str; char t[16][16]; int d[14][14][14][14][14][14]; int sy[3],sx[3],ty[3],tx[3]; struct state{ int y0,x0,y1,x1,y2,x2; }; int solve(){ state s; s.y0=sy[0];s.x0=sx[0]; s.y1=sy[1];s.x1=sx[1]; s.y2=sy[2];s.x2=sx[2]; for(int i=0;i<H-2;i++) for(int j=0;j<W-2;j++) for(int i2=0;i2<H-2;i2++) for(int j2=0;j2<W-2;j2++) for(int i3=0;i3<H-2;i3++) for(int j3=0;j3<W-2;j3++) d[i][j][i2][j2][i3][j3]=1e9; d[s.y0][s.x0][s.y1][s.x1][s.y2][s.x2]=0; queue< state > Q; Q.push(s); while(!Q.empty()){ //if(Tc++ >10) break; s=Q.front();Q.pop(); int cost=d[s.y0][s.x0][s.y1][s.x1][s.y2][s.x2]; int y0=s.y0,x0=s.x0; int y1=s.y1,x1=s.x1; int y2=s.y2,x2=s.x2; /* cout<<cost<<' '<<y0<<' '<<x0<<' '<<y1<<' '<<x1<<endl; for(int i=0;i<H-1;i++){ for(int j=0;j<W-1;j++){ if(i==y0&&j==x0)cout<<'a'; else if(i==y1&&j==x1)cout<<'b'; else if(i==y2&&j==x2)cout<<'c'; else if('a'<=t[i][j]&&t[i][j]<='c')cout<<' '; else if('A'<=t[i][j]&&t[i][j]<='C')cout<<' '; else cout<<t[i][j]; } cout<<endl; } cout<<endl; */ bool flg=true; if(s.y0!=ty[0])flg=false; if(s.x0!=tx[0])flg=false; if(s.y1!=ty[1]&&N>=2)flg=false; if(s.x1!=tx[1]&&N>=2)flg=false; if(s.y2!=ty[2]&&N>=3)flg=false; if(s.x2!=tx[2]&&N>=3)flg=false; if(flg)return cost; for(int i=0;i<5;i++){ if(N==1&&i==0)continue; int ny0=s.y0+dy[i]; int nx0=s.x0+dx[i]; if(ny0 < 0 || nx0 < 0 || ny0 >=H-2 || nx0 >=W-2 )continue; if(t[ny0][nx0]=='#')continue; for(int j=0;j<5;j++){ if(N==1&&j>0)continue; int ny1=s.y1+dy[j]; int nx1=s.x1+dx[j]; if(ny1 < 0 || nx1 < 0 || ny1 >=H-2 || nx1 >=W-2 )continue; if(t[ny1][nx1]=='#')continue; for(int k=0;k<5;k++){ if(N<=2&&k>0)continue; int ny2=s.y2+dy[k]; int nx2=s.x2+dx[k]; if(ny2 < 0 || nx2 < 0 || ny2 >=H-2 || nx2 >=W-2 )continue; if(t[ny2][nx2]=='#')continue; if( ny0 == ny1 && nx0 == nx1 &&N>=2) continue; if( ny1 == ny2 && nx1 == nx2 &&N==3) continue; if( ny2 == ny0 && nx2 == nx0 &&N==3) continue; if( ny0 == s.y1 && nx0 == s.x1 && ny1 == s.y0 && nx1 == s.x0 &&N>=2)continue; if( ny1 == s.y2 && nx1 == s.x2 && ny2 == s.y1 && nx2 == s.x1 &&N==3)continue; if( ny2 == s.y0 && nx2 == s.x0 && ny0 == s.y2 && nx0 == s.x2 &&N==3)continue; /* if(ny0==1 && nx0==1 && ny1==1 && nx1==2){ cout<<y0<<' '<<x0<<' '<<y1<<' '<<x1<<' '<<'!'<<endl; } */ if( d[ny0][nx0][ny1][nx1][ny2][nx2]>cost+1){ d[ny0][nx0][ny1][nx1][ny2][nx2]=cost+1; Q.push( (state){ny0,nx0,ny1,nx1,ny2,nx2} ); } } } } } return -1; } int main(){ while(1){ cin>>W>>H>>N; if(H==0&&W==0&&N==0)break; cin.ignore(); for(int i=0;i<3;i++) sy[i]=sx[i]=ty[i]=tx[i]=0; for(int i=0;i<H;i++){ getline(cin,str); for(int j=0;j<W;j++){ if(i>0&&j>0){ t[i-1][j-1]=str[j]; if('a'<=str[j]&&str[j]<='c'){ sy[ str[j]-'a' ]=i-1; sx[ str[j]-'a' ]=j-1; } if('A'<=str[j]&&str[j]<='C'){ ty[ str[j]-'A' ]=i-1; tx[ str[j]-'A' ]=j-1; } } } } cout<<solve()<<endl; } return 0; }
#include <iostream> #include <vector> #include <string> #include <cstring> using namespace std; bool wall[16*16]; int mv[] = {0, 1, -1, 16, -16}; int compress(int pa, int pb, int pc){ return (pa)|(pb<<8)|(pc<<16); } int solve(int start, int goal){ static bool visit[16*16*16*16*16*16]; memset(visit, false, sizeof(visit)); visit[start] = true; vector<int> state[2]; state[0].push_back(start); for(int res=1; ;res++){ int cur = 1-res%2, next = res%2; state[next].clear(); for(int i=0;i<state[cur].size();i++){ int pa = state[cur][i]%256, pb = (state[cur][i]>>8)%256, pc = (state[cur][i]>>16); for(int a=0;a<5;a++){ int npa = pa + mv[a]; if(wall[npa]) continue; for(int b=0;b<(pb?5:1);b++){ int npb = pb + mv[b]; if(npb!=0){ if(wall[npb]) continue; if(npa==npb) continue; if(npb==pa&&npa==pb) continue; } for(int c=0;c<(pc?5:1);c++){ int npc = pc + mv[c]; if(npc!=0){ if(wall[npc]) continue; if(npc==npa||npc==npb) continue; if(npc==pa&&npa==pc) continue; if(npc==pb&&npb==pc) continue; } int ns = (npa)|(npb<<8)|(npc<<16); if(visit[ns]) continue; if(ns==goal) return res; visit[ns] = true; state[next].push_back(ns); } } } } } return -1; } int main(){ int w, h, n; string mp[16]; while(cin >> w >> h >> n, w){ getline(cin, mp[0]); for(int i=0;i<h;i++) getline(cin, mp[i]); memset(wall, false, sizeof(wall)); int start = 0, goal = 0; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(mp[i][j]=='#') wall[16*i+j] = true; if(islower(mp[i][j])) start |= (16*i+j) << (8*(mp[i][j]-'a')); if(isupper(mp[i][j])) goal |= (16*i+j) << (8*(mp[i][j]-'A')); } } printf("%d\n", solve(start, goal)); } }
#include<iostream> #include<cmath> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) const int MOD = 100000007; long long nck[1111][1111]; int main() { rep (i, 1111) nck[i][0] = 1; rep (i, 1110) rep (j, 1110) { nck[i + 1][j + 1] = (nck[i][j] + nck[i][j + 1]) % MOD; } int r, c, a1, a2, b1, b2, t = 1; cin >> r >> c >> a1 >> a2 >> b1 >> b2; if (r == abs(a1 - b1) * 2) t *= 2; if (c == abs(a2 - b2) * 2) t *= 2; int aa = min(abs(a1 - b1), r - abs(a1 - b1)); int bb = min(abs(a2 - b2), c - abs(a2 - b2)); cout << nck[aa + bb][aa] * t % MOD << endl; return 0; }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> #include <list> #include <cctype> #include <utility> using namespace std; typedef long long ll; typedef pair <int,int> P; typedef pair <int,P > PP; int tx[] = {0,1,0,-1}; int ty[] = {-1,0,1,0}; static const double EPS = 1e-8; int dp[1001][1001]; int ComputeDistance(int x1,int x2,int w){ // ..x1....x2.. dist = min(x2-x1,(col-x2)+x1); // ..x2....x1.. dist = min(x1-x2,(col-x1)+x2); return x1 < x2 ? min(x2-x1,(w-x2)+x1) : min(x1-x2,(w-x1)+x2); } int ComputeCandidateNum(int x1,int x2,int w){ if(x1 < x2){ if(x2-x1 != (w-x2)+x1){ return 1; } else{ return 2; } } else{ if(x1-x2 != (w-x1)+x2){ return 1; } else{ return 2; } } } int main(){ int row,col; int x1,y1,x2,y2; while(~scanf("%d %d %d %d %d %d",&row,&col,&x1,&y1,&x2,&y2)){ memset(dp,0,sizeof(dp)); int x_dist=ComputeDistance(x1,x2,row); int x_candidate_num = ComputeCandidateNum(x1,x2,row); int y_dist=ComputeDistance(y1,y2,col); int y_candidate_num = ComputeCandidateNum(y1,y2,col); for(int x=0;x<=x_dist;x++) dp[0][x] = 1; for(int y=0;y<=y_dist;y++) dp[y][0] = 1; for(int y=0;y<y_dist;y++){ for(int x=0;x<x_dist;x++){ dp[y+1][x+1] = max((dp[y][x+1] + dp[y+1][x]) % 100000007,dp[y+1][x+1]); } } printf("%d\n", dp[y_dist][x_dist]*x_candidate_num*y_candidate_num % 100000007); } }
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; } inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 100000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; ll extgcd(ll a, ll b, ll &x, ll &y){ int d = a; if(b != 0){ d = extgcd(b, a % b, y, x); y -= (a / b) * x; }else{ x = 1; y = 0; } return d; } ll mod_inv(ll a, ll M){ ll x, y; extgcd(a, M, x, y); return (M + x % M) % M; } ll FACT[2001], IFACT[2001]; ll binomial(ll n, ll r){ if(n < 0 || r < 0 || r > n)return 0; if(r > n / 2)r = n - r; return FACT[n]*IFACT[n-r]%MOD*IFACT[r]%MOD; } int main(){ FACT[0] = 1; IFACT[0] = 1; for(int i = 1; i <= 2000; i++){ FACT[i] = (FACT[i - 1] * i) % MOD; IFACT[i] = mod_inv(FACT[i], MOD); } int r, c, a1, a2, b1, b2; ll k = 1; cin>>r>>c>>a1>>a2>>b1>>b2; int y = abs(b1 - a1); if(y == r - y) k *= 2; else if(y > r - y) y = r - y; int x = abs(b2 - a2); if(x == c - x) k *= 2; else if(x > c - x) x = c - x; cout<<(k * binomial(x + y, x)) % MOD<<endl; return 0; }
#include<iostream> #include<cstdlib> #include<algorithm> using namespace std; #define MOD 100000007 #define MAXN 2000 long long inv[MAXN+1];//MODを法とする乗法の逆元 long long fact[MAXN+1];//階乗 long long ifact[MAXN+1];//階乗の逆元 void init(){ inv[1] = 1; for(int i=2;i<=MAXN;i++) inv[i] = inv[MOD%i] * (MOD - MOD/i) % MOD; fact[0]=ifact[0]=1; for(int i=1;i<=MAXN;i++){ fact[i]=i*fact[i-1]%MOD; ifact[i]=ifact[i-1]*inv[i]%MOD; } } long long C(int n, int r){ if(n < 0 || r < 0 || r > n)return 0; if(r > n / 2)r = n - r; return fact[n]*ifact[n-r]%MOD*ifact[r]%MOD; } int main(){ int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2; int x,y; x = min(abs(a1-b1) , min(a1,b1)+r-max(a1,b1)); y = min(abs(a2-b2) , min(a2,b2)+c-max(a2,b2)); init(); int ans=C(x+y,x); if(abs(a1-b1) == min(a1,b1)+r-max(a1,b1))ans=(ans*2)%MOD; if(abs(a2-b2) == min(a2,b2)+c-max(a2,b2))ans=(ans*2)%MOD; cout<<ans<<endl; return 0; }
#include<iostream> #include<vector> #include<string> #include<map> #include<algorithm> #include<functional> #define all(c) c.begin(),c.end() #define uni(c) c.erase(unique(c.begin(),c.end()),c.end()) #define pb push_back using namespace std; //typedef __int64 ll; typedef long long int ll; ll mod=100000007; int comb[2003][2003]; void Comb(int c){ for(int i=0;i<c;i++){ for(int j=0;j<c;j++){ comb[i][j]=0; } } comb[0][0]=1; for(int i=1;i<c;i++){ comb[i][0]=1; for(int j=1;j<i+1;j++){ comb[i][j]=comb[i-1][j]+comb[i-1][j-1]; comb[i][j]%= mod; } } } int main(){ int r,c,x1,y1,x2,y2; Comb(2002); while(cin>>r>>c>>x1>>y1>>x2>>y2){ r--;c--; int a[]={abs(x1-x2),abs(x1-r)+1+x2,abs(x2-r)+1+x1}; int b[]={abs(y1-y2),abs(y1-c)+1+y2,abs(y2-c)+1+y1}; int sa=5000,sb=5000; for(int i=0;i<3;i++){ sa=min(sa,a[i]); sb=min(sb,b[i]); //cout<<" "<<a[i]<<" "<<b[i]<<endl; } int ca=0,cb=0; for(int i=0;i<3;i++){ if(a[i]==sa) ca++; if(b[i]==sb) cb++; } int x=ca*cb%mod; ll ans=(ll)(comb[sa+sb][sa])*(ll)x%mod; cout<<ans<<endl; } return 0; }
#include<iostream> #include<vector> using namespace std; typedef long long ll; const int mod = 100000007; struct Combination{ int n; vector<vector<int>> dp; Combination(int i){ n = i; dp = vector<vector<int>>(n+1, vector<int>(n+1, 0)); constructTriangle(); } void constructTriangle(){ dp[0][0] = 1; for(int i = 1; i <= n; i++){ dp[i][0] = dp[i-1][0]; for(int j = 1; j <= i; j++){ dp[i][j] = (dp[i-1][j] + dp[i-1][j-1]) % mod; } } } // return aCb int getCombination(int a, int b){ return dp[a][b]; } }; int main(){ Combination comb(1001); int r, c, ax, ay, bx, by; cin >> r >> c >> ax >> ay >> bx >> by; int mind = 1001; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ int nx = bx + i*r, ny = by + j*c; mind = min(mind, abs(nx-ax) + abs(ny-ay)); } } ll ans = 0; for(int i = -1; i <= 1; i++){ for(int j = -1; j <= 1; j++){ int nx = bx + i*r, ny = by + j*c; if(abs(nx-ax)+abs(ny-ay) != mind) continue; int x = abs(nx-ax), y = abs(ny-ay); ans += comb.getCombination(x+y, x); ans %= mod; } } cout << ans << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; const int N = 3000; const int M = 100000007; int C[N+1][N+1]; void init(){ for(int i=0;i<N;i++){ C[i][0] = C[i][i] = 1; for(int j=0;j<i;j++) C[i][j] = (C[i-1][j-1] + C[i-1][j]) % M; } } int main(){ int r, c, sy, sx, ty, tx, by, bx; init(); int dy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; cin >> r >> c >> sy >> sx >> ty >> tx; int ans = 0; by = bx = M; for(int i=0;i<9;i++){ int ny = abs(ty + r * dy[i] - sy); int nx = abs(tx + c * dx[i] - sx); if(by + bx > ny + nx){ by = ny; bx = nx; ans = C[by+bx][by]; }else if(by + bx == ny + nx){ ans = (ans + C[ny+nx][ny]) % M; } } cout << ans << endl; }
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <set> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,j) FOR(i,0,j) #define mp std::make_pair const int INF = 1 << 24; // S N E W(南北東西) const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; typedef long long ll; typedef unsigned long long ull; typedef std::pair<ll,ll> P; typedef std::pair<ll,P> State; const ll MOD = 100000007; // 2 3 -> (1, -1, 1) // 6 27 -> (3, -4, 1) State extgcd(ll a, ll b){ if(b == 0){ return mp(a, mp(1, 0)); } State s = extgcd(b, a%b); P p = s.second; return mp(s.first, mp(p.second, p.first-(a/b)*p.second)); } // 3 7 -> 5 // 11 7 -> 2 ll mod_inverse(ll a, ll m){ State s = extgcd(a, m); return (m + s.second.first % m) % m; } ll fact[2001]; P mod_fact(ll n, ll p){ int e = 0, _n = n; while(_n > 0){ e += _n / p; _n /= p; } if(n / p % 2 == 0){return mp(fact[n/p] * fact[n%p] % p, e);} return mp(fact[n/p] * (p - fact[n%p]) % p, e); } ll mod_combination(ll n, ll k, ll p){ if(n < 0 || k < 0 || n < k){return 0;} P p1 = mod_fact(n, p), p2 = mod_fact(k, p), p3 = mod_fact(n-k, p); if(p1.second > p2.second + p3.second){return 0;} return p1.first * mod_inverse(p2.first * p3.first % p, p) % p; } int main(){ fact[0] = 1; FOR(i, 1, 2001){ fact[i] = (fact[i-1] * i) % MOD; } int r, c, a1, a2, b1, b2; std::cin >> r >> c >> a1 >> a2 >> b1 >> b2; if(a1 > b1){std::swap(a1, b1);} if(a2 > b2){std::swap(a2, b2);} int dx[2] = {b1-a1, a1+r-b1}, dy[2] = {b2-a2, a2+c-b2}; int shortestPath = INF; REP(i, 2){ REP(j, 2){ shortestPath = std::min(shortestPath, dx[i]+dy[j]); } } ll res = 0; REP(i, 2){ REP(j, 2){ if(dx[i] + dy[j] == shortestPath){ res = (res + mod_combination(dx[i]+dy[j], dx[i], MOD)) % MOD; } } } std::cout << res << std::endl; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second typedef long long ll; typedef pair<ll,ll> P; typedef pair<ll,P> PP; int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1}; int main() { int n,m,x1,y1,x2,y2,mod=100000007; cin >> n >> m >> x1 >> y1 >> x2 >> y2; ll d[n][m],c[n][m]; memset(c,0,sizeof(c)); for(int i=0;i<n;i++)for(int j=0;j<m;j++)d[i][j]=mod; d[x1][y1]=0; c[x1][y1]=1; priority_queue<PP,vector<PP>,greater<PP> > que; que.push(PP(0,P(x1,y1))); while(!que.empty()) { PP p=que.top();que.pop(); int xx=p.S.F,yy=p.S.S,cc=p.F; if(d[xx][yy]<cc) continue; for(int i=0; i<4; i++) { int x=xx+dx[i],y=yy+dy[i]; if(x<0) x=n-1; if(x>=n) x=0; if(y<0) y=m-1; if(y>=m) y=0; if(d[x][y]>d[xx][yy]+1) { c[x][y]=c[xx][yy]; d[x][y]=d[xx][yy]+1; que.push(PP(d[x][y],P(x,y))); } else if(d[x][y]==d[xx][yy]+1) { c[x][y]+=c[xx][yy]; c[x][y]%=mod; } } } cout << c[x2][y2] << endl; return 0; }
#include <bits/stdc++.h> #define mod (100000007) using namespace std; typedef long long ll; struct po{ll x,y;}; int dx[]={0,0,-1,1}; int dy[]={1,-1,0,0}; ll w,h,a1,a2,b1,b2; void Add(ll &a,ll b){a=(a+b)%mod;} po D[1001][1001]; ll bfs(po s,po g){ for(int i=0;i<1001;i++) for(int j=0;j<1001;j++) D[i][j]=(po){-1,0}; queue<po> Q; Q.push(s); D[s.y][s.x]=(po){0,1}; while(!Q.empty()){ po t=Q.front();Q.pop(); for(int i=0;i<4;i++){ int nx=(w+t.x+dx[i])%w; int ny=(h+t.y+dy[i])%h; if(D[ny][nx].x==D[t.y][t.x].x+1)Add(D[ny][nx].y,D[t.y][t.x].y); else if(D[ny][nx].x==-1){ D[ny][nx]=D[t.y][t.x]; D[ny][nx].x++; Q.push((po){nx,ny}); } } } return D[g.y][g.x].y%mod; } int main(){ cin>>w>>h>>a1>>a2>>b1>>b2; cout<<bfs((po){a1,a2},(po){b1,b2})<<endl;; }
#include <cassert> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const int MOD = 100 * 1000 * 1000 + 7; ll dp[2001][2001]; ll comb(int n, int k) { assert(n >= k); assert(n > 0); if (dp[n][k] > 0) { return dp[n][k]; } else if (k == 1) { return n; } else if (k == 0) { return 1; } else if (n == k) { return 1; } dp[n][k] = (comb(n - 1, k - 1) + comb(n - 1, k)) % MOD; return dp[n][k]; } int main(void) { int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; int m = min(abs(b1 - a1), r - abs(b1 - a1)); int n = min(abs(b2 - a2), c - abs(b2 - a2)); ll answer = comb(m + n, m); if (r % 2 == 0 && m == r / 2) { answer = 2 * answer % MOD; } if (c % 2 == 0 && n == c / 2) { answer = 2 * answer % MOD; } cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = ll(1e8+7); const ll MAX_N = ll(1e5 + 5); #define REP(i, n) for (int i = 0; i < n; i++) //階乗と逆元を保持 ll factrial[MAX_N], inverse[MAX_N]; //繰り返し二乗法 ll mod_power(ll x, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } //前処理 void init(ll n) { factrial[0] = 1; inverse[0] = 1; for (ll i = 1; i <= n; i++) { //階乗を求める factrial[i] = (factrial[i - 1] * i) % MOD; //フェルマーの小定理で逆元を求める inverse[i] = mod_power(factrial[i], MOD - 2) % MOD; } } //クエリ ll nCk(ll n, ll k) { if(n < 0 || k < 0 || n < k) return 0; return factrial[n] * inverse[k] % MOD * inverse[n - k] % MOD; } int main() { ll r,c,ai,aj,bi,bj; cin >> r >> c >> ai >> aj >> bi >> bj; vector<P> bs, mins; for(int i = -1; i<=1; ++i){ for(int j = -1; j<=1; ++j){ bs.push_back({bi+r*i,bj+c*j}); } } ll dmin = 99999999; for(auto p : bs){ if(abs(p.first - ai) + abs(p.second - aj) < dmin){ mins.clear(); mins.push_back({abs(p.first - ai), abs(p.second - aj)}); dmin = abs(p.first - ai) + abs(p.second - aj); } else if(abs(p.first - ai) + abs(p.second - aj) == dmin){ mins.push_back({abs(p.first - ai), abs(p.second - aj)}); } } init(dmin); ll ans = 0; for(auto p : mins){ ans += nCk(dmin, p.first); ans %= MOD; } cout << ans << endl; return 0; }
#include <cmath> #include <ctime> #include <algorithm> #include <iostream> #include <map> #include <numeric> #include <set> #include <sstream> #include <string> #include <vector> #include <list> #include <deque> #include <stack> #include <bitset> #include <functional> #include <numeric> #include <utility> #include <iomanip> #include <cstdio> #include <cctype> #include <queue> #include <complex> #include <climits> #include <cstring> typedef long long ll; using namespace std; ll nCr[2000][2000] = {0}; int main(void){ for(int i=1; i<1010; ++i){ nCr[i][0] = nCr[i][i] = 1; } for(int i=2; i<1010; ++i){ for(int j=1; j<i; ++j){ nCr[i][j] = (nCr[i-1][j-1] + nCr[i-1][j]) % 100000007; } } int r, c, a1, a2, b1, b2; int min_1, min_2, coef = 1; cin >> r >> c >> a1 >> a2 >> b1 >> b2; min_1 = min(abs(a1-b1), r-abs(a1-b1)); min_2 = min(abs(a2-b2), c-abs(a2-b2)); if(abs(a1-b1) == r-abs(a1-b1)) coef *= 2; if(abs(a2-b2) == c-abs(a2-b2)) coef *= 2; int res; if(min_1+min_2 == 0){ res = 1; } else { res =(coef * (int)nCr[min_1+min_2][min_2]) % 100000007 ; } cout << res << endl; return 0; }
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> #include <utility> #include <ctime> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <cstring> #include <numeric> #include <cassert> using namespace std; static const double EPS = 1e-10; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define rev(i,n) for(int i=n-1;i>=0;i--) #define all(a) a.begin(),a.end() #define mp(a,b) make_pair(a,b) #define pb(a) push_back(a) #define SS stringstream #define DBG1(a) rep(_X,sz(a)){printf("%d ",a[_X]);}puts(""); #define DBG2(a) rep(_X,sz(a)){rep(_Y,sz(a[_X]))printf("%d ",a[_X][_Y]);puts("");} #define bitcount(b) __builtin_popcount(b) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define delete(a,n) a.erase(remove(all(a),n),a.end()) template<typename T, typename S> vector<T>& operator<<(vector<T>& a, S b) { a.push_back(b); return a; } template<typename T> void operator>>(vector<T>& a, int b) {while(b--)if(!a.empty())a.pop_back();} bool isprime(int n){ if(n<2)return false; for(int i=2;i*i<=n;i++)if(n%i==0)return false; return true;} ll b_pow(ll x,ll n){return n ? b_pow(x*x,n/2)*(n%2?x:1) : 1ll;} string itos(int n){stringstream ss;ss << n;return ss.str();} int dp[3000][3000]; int main(){ dp[0][0] = 1; for(int i = 0 ; i < 2500 ; i++){ for(int j = 0 ; j < 2500 ; j++){ dp[i+1][j] += dp[i][j]; dp[i+1][j+1] += dp[i][j]; dp[i+1][j] %= 100000007; dp[i+1][j+1] %= 100000007; } } ios_base::sync_with_stdio(false); int r,c,y1,x1,y2,x2; cin >> r >> c >> y1 >> x1 >> y2 >> x2; if( x1 == x2 && y1 == y2 ){ cout << 1 << endl; return 0; } int rd = (y1<=y2?y2-y1:r-y1+y2)+(x1<=x2?x2-x1:c-x1+x2); int ld = (y1<=y2?y2-y1:r-y1+y2)+(x1>=x2?x1-x2:x1+c-x2); int ru = (y1>=y2?y1-y2:r-y2+y1)+(x1<=x2?x2-x1:c-x1+x2); int lu = (y1>=y2?y1-y2:r-y2+y1)+(x1>=x2?x1-x2:x1+c-x2); int mi = min( min(rd,ld) , min(ru,lu) ); long long ans = 0; if( mi == rd ){ int a = (y1<=y2?y2-y1:r-y1+y2); int b = (x1<=x2?x2-x1:c-x1+x2); ans += dp[a+b][a]; } if( x1 != x2 && mi == ld ){ int a = (y1<=y2?y2-y1:r-y1+y2); int b = (x1>=x2?x1-x2:x1+c-x2); ans += dp[a+b][a]; } if(y1 != y2 && mi == ru ){ int a = (y1>=y2?y1-y2:r-y2+y1); int b = (x1<=x2?x2-x1:c-x1+x2); ans += dp[a+b][a]; } if(y1 != y2 && x1 != x2 && mi == lu ){ int a = (y1>=y2?y1-y2:r-y2+y1); int b = (x1>=x2?x1-x2:x1+c-x2); ans += dp[a+b][a]; } cout << ans%100000007 << endl; }
#include<stdio.h> #include<algorithm> using namespace std; #define MOD 100000007; int conbis[3000][3000]; int conbination(int a, int b){ if(conbis[a][b] != 0)return conbis[a][b]; if(a == b || b == 0)return conbis[a][b] = 1; conbis[a][b] = (conbination(a - 1, b) + conbination(a - 1, b - 1)) % MOD; return conbis[a][b]; } int main(){ int r, c, a1, a2, b1, b2, x, y; scanf("%d%d%d%d%d%d", &r, &c, &a1, &a2, &b1, &b2); long long int ans = 1; x = abs(a1 - b1); x = min(x, r - x); y = abs(a2 - b2); y = min(y, c - y); if(r % 2 == 0 && r / 2 == x) ans *= 2; if(c % 2 == 0 && c / 2 == y) ans *= 2; ans *= conbination(x + y, y); ans %= MOD; printf("%lld\n", ans); }
#include <bits/stdc++.h> using namespace std; constexpr int INF = (1 << 29); constexpr int MOD = ((1e8) + 7); int main() { int r, c, x1, y1, x2, y2; cin >> r >> c >> y1 >> x1 >> y2 >> x2; vector<vector<int>> d(r, vector<int>(c, INF)); vector<vector<int>> e(r, vector<int>(c, 0)); d[y1][x1] = 0; e[y1][x1] = 1; constexpr int dx[] = {-1, +0, +1, +0}; constexpr int dy[] = {+0, -1, +0, +1}; queue<pair<int, int>> que; que.push(make_pair(y1, x1)); while (!que.empty()) { int x, y; tie(y, x) = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int ny = (y + dy[i] + r) % r; int nx = (x + dx[i] + c) % c; if (d[y][x] + 1 <= d[ny][nx]) { e[ny][nx] += e[y][x]; e[ny][nx] %= MOD; } if (d[y][x] + 1 < d[ny][nx]) { d[ny][nx] = d[y][x] + 1; que.push(make_pair(ny, nx)); } } } cout << e[y2][x2] << endl; return 0; }
#include <bits/stdc++.h> // #define rep(i,n) for(int i=0;i<n;i++) // #define all(x) (x).begin(),(x).end() using namespace std; // const int INF=1145141919,MOD=1e9+7; // const long long LINF=8931145141919364364,LMOD=998244353; // inline long long mod(long long n,long long m){return(n%m+m)%m;} // const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1}; struct Combination{ const int MAX=2010; const int MOD; vector<vector<int>> COM; vector<vector<int>> PER; Combination(const int MOD): MOD(MOD) { COM=vector<vector<int>>(MAX,vector<int>(MAX)); PER=vector<vector<int>>(MAX,vector<int>(MAX)); COM[0][0]=PER[0][0]=1; for(int i=1;i<MAX;i++){ COM[i][0]=PER[i][0]=1; for(int j=1;j<MAX;j++){ COM[i][j]=(COM[i-1][j-1]+COM[i-1][j])%MOD; PER[i][j]=PER[i][j-1]*(i-j+1)%MOD; } } } int C(int n,int r){ assert(0<=n&&n<MAX&&0<=r&&r<MAX); return COM[n][r]; } int P(int n,int r){ assert(0<=n&&n<MAX&&0<=r&&r<MAX); return PER[n][r]; } int H(int n,int r){ assert(0<=n+r-1&&n+r-1<MAX&&0<=r&&r<MAX); return COM[n+r-1][r]; } }; int main(){ const int MOD=100000007; Combination com(MOD); int r,c,a1,a2,b1,b2; cin>>r>>c>>a1>>a2>>b1>>b2; int h1=abs(a1-b1),h2=r-abs(a1-b1); int h=min(h1,h2); int w1=abs(a2-b2),w2=c-abs(a2-b2); int w=min(w1,w2); int ans=com.C(h+w,h); if(h1==h2) ans=ans*2%MOD; if(w1==w2) ans=ans*2%MOD; cout<<ans<<endl; return 0; }
#include <iostream> #include <sstream> #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <cstring> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <numeric> #include <utility> #include <iomanip> #include <algorithm> #include <functional> using namespace std; typedef long long ll; typedef vector<int> vint; typedef vector<long long> vll; typedef pair<int,int> pint; typedef pair<long long, long long> pll; #define MP make_pair #define PB push_back #define ALL(s) (s).begin(),(s).end() #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } const long long MOD = 100000007; const int MAX_C = 2000; long long Com[MAX_C][MAX_C]; void calc_com() { memset(Com, 0, sizeof(Com)); Com[0][0] = 1; for (int i = 1; i < MAX_C; ++i) { Com[i][0] = 1; for (int j = 1; j < MAX_C; ++j) { Com[i][j] = (Com[i-1][j-1] + Com[i-1][j]) % MOD; } } } int r, c, a1, a2, b1, b2; int main() { calc_com(); while (cin >> r >> c >> a1 >> a2 >> b1 >> b2) { int fx = 1, fy = 1; int x1 = abs(a1 - b1), x2 = r - x1, y1 = abs(a2 - b2), y2 = c - y1; if (x1 == x2) ++fx; if (y1 == y2) ++fy; int x = min(x1, x2), y = min(y1, y2); long long res = (Com[x+y][x] * fx * fy) % MOD; cout << res << endl; } return 0; }
#line 1 "test/algebra/combination.test.cpp" #define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1501" #include <bits/stdc++.h> using namespace std; using lint = long long; const lint mod = 100000007; #line 1 "test/algebra/../../library/algebra/combination.cpp" struct Combination { vector<lint> fac, finv, inv; Combination(lint maxN) : fac(maxN + 100), finv(maxN + 100), inv(maxN + 100) { maxN += 100; // for safety fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (lint i = 2; i <= maxN; ++i) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } lint operator()(lint n, lint k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } }; #line 9 "test/algebra/combination.test.cpp" int main() { cin.tie(0); ios::sync_with_stdio(false); int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; if (a1 > b1) swap(a1, b1); if (a2 > b2) swap(a2, b2); int mult = 1; int h1 = b1 - a1; int w1 = b2 - a2; int h2 = a1 - b1 + r; int w2 = a2 - b2 + c; if (h1 == h2) mult *= 2; if (w1 == w2) mult *= 2; int h = min(h1, h2); int w = min(w1, w2); Combination nCk(2000); cout << (nCk(h + w, w) * mult) % mod << "\n"; return 0; }
#include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <stack> #include <queue> #include <algorithm> #include <set> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,j) FOR(i,0,j) #define mp std::make_pair const int INF = 1 << 24; // S N E W(南北東西) const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1}; typedef long long ll; typedef unsigned long long ull; typedef std::pair<ll,ll> P; typedef std::pair<ll,P> State; const ll MOD = 100000007; // 2 3 -> (1, -1, 1) // 6 27 -> (3, -4, 1) State extgcd(ll a, ll b){ if(b == 0){ return mp(a, mp(1, 0)); } State s = extgcd(b, a%b); P p = s.second; return mp(s.first, mp(p.second, p.first-(a/b)*p.second)); } // 3 7 -> 5 // 11 7 -> 2 ll mod_inverse(ll a, ll m){ State s = extgcd(a, m); return (m + s.second.first % m) % m; } ll fact[2001]; P mod_fact(ll n, ll p){ if(n == 0){return mp(1, 0);} ll e = n / p; P small = mod_fact(n/p, p); e += small.second; if(n / p % 2 == 0){return mp(small.first * fact[n%p] % p, e);} return mp(small.first * (p - fact[n%p]) % p, e); } ll mod_combination(ll n, ll k, ll p){ if(n < 0 || k < 0 || n < k){return 0;} P p1 = mod_fact(n, p), p2 = mod_fact(k, p), p3 = mod_fact(n-k, p); if(p1.second > p2.second + p3.second){return 0;} return p1.first * mod_inverse(p2.first * p3.first % p, p) % p; } int main(){ fact[0] = 1; FOR(i, 1, 2001){ fact[i] = (fact[i-1] * i) % MOD; } int r, c, a1, a2, b1, b2; std::cin >> r >> c >> a1 >> a2 >> b1 >> b2; if(a1 > b1){std::swap(a1, b1);} if(a2 > b2){std::swap(a2, b2);} int dx[2] = {b1-a1, a1+r-b1}, dy[2] = {b2-a2, a2+c-b2}; int shortestPath = INF; REP(i, 2){ REP(j, 2){ shortestPath = std::min(shortestPath, dx[i]+dy[j]); } } ll res = 0; REP(i, 2){ REP(j, 2){ if(dx[i] + dy[j] == shortestPath){ res = (res + mod_combination(dx[i]+dy[j], dx[i], MOD)) % MOD; } } } std::cout << res << std::endl; }
#include<iostream> #include<algorithm> #include<cmath> #define N 1001 #define M 100000007 using namespace std; typedef long long ll; ll dp[N][N]; void init(){ for(int i=0; i<N; ++i){ dp[i][0] = 1; dp[0][i] = 1; } for(int i=1; i<N; ++i){ for(int j=i; j<N; ++j){ dp[i][j] = (dp[i][j-1] + dp[i-1][j]) % M; dp[j][i] = (dp[j][i-1] + dp[j-1][i]) % M; } } } int main(){ int n, m, x, y; int x1, y1, x1_, y1_, x2, y2; int ax, ay, bx, by; ll ans; init(); cin >> n >> m >> x1 >> y1 >> x2 >> y2; if(x1 < x2) x1_ = x1 + n; else x1_ = x1 - n; if(y1 < y2) y1_ = y1 + m; else y1_ = y1 - m; ax = abs(x1 - x2); ay = abs(y1 - y2); bx = abs(x1_ - x2); by = abs(y1_ - y2); x = min(ax, bx); y = min(ay, by); ans = dp[x][y]; if(ax == bx && ax != 0) ans *= 2; if(ay == by && ay != 0) ans *= 2; cout << ans%M << endl; return 0; }
#include <iostream> #include <queue> #include <climits> using namespace std; #define MOD 100000007 typedef pair<int,int> P; int h, w; int sx, sy, gx, gy; int closed[1002][1002]; int minDist[1002][1002]; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int main(){ while(cin >> h >> w >> sy >> sx >> gy >> gx){ queue<P> open; open.push(P(sx, sy)); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ minDist[i][j] = INT_MAX; closed[i][j] = 0; } } closed[sy][sx] = 1; minDist[sy][sx] = 0; while(!open.empty()){ P p = open.front(); open.pop(); if(p.first == gx && p.second == gy){ break; } int ncost = minDist[p.second][p.first] + 1; for(int i = 0; i < 4; i++){ int nx = (p.first + dx[i] + w) % w; int ny = (p.second + dy[i] + h) % h; if(minDist[ny][nx] < ncost) continue; if(minDist[ny][nx] > ncost) open.push(P(nx, ny)); closed[ny][nx] += closed[p.second][p.first]; closed[ny][nx] %= MOD; minDist[ny][nx] = ncost; } } cout << closed[gy][gx] << endl; } }
#include <iostream> #include <algorithm> #define N 100000007 using namespace std; int inverse(int x) { int n = N - 2; long long int a = x; long long int y = 1; while (n > 0) { if (n % 2 > 0) { y = y * a % N; } a = a * a % N; n /= 2; } return y; } int combination(int n, int k) { long long int c = 1; for (int i = 0; i < min(k, n - k); ++i) { c = c * (n - i) * inverse(i + 1) % N; } return c; } int main() { int r, c, a1, a2, b1, b2, w, h; long long int k = 1; cin >> r >> c >> a1 >> a2 >> b1 >> b2; w = min(abs(a1 - b1), r - abs(a1 - b1)); h = min(abs(a2 - b2), c - abs(a2 - b2)); if (w * 2 == r) { k *= 2; } if (h * 2 == c) { k *= 2; } cout << combination(w + h, w) * k % N << endl; return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<cctype> #include<math.h> #include<string> #include<string.h> #include<stack> #include<queue> #include<vector> #include<utility> #include<set> #include<map> #include<stdlib.h> #include<iomanip> using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define LINF (ll)INF*INF #define MOD 100000007 #define rep(i,n) for(int i=0;i<(n);i++) #define loop(i,a,n) for(int i=a;i<(n);i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! typedef vector<int> vi; typedef vector<string> vs; typedef pair<int,int> pii; typedef vector<pii> vp; int gcd(int a, int b){ if(b==0) return a; return gcd(b,a%b); } int lcm(int a, int b){ return a/gcd(a,b)*b; } #define MAX 3000 int ans = INF; vp v; int ncr[MAX][MAX]; void f(int sx, int sy, int gx, int gy){ int dist = abs(sx-gx) + abs(sy-gy); if(dist > ans)return; if(dist < ans){ ans = dist; v.clear(); } v.push_back(pii(abs(sx-gx),abs(sy-gy))); } signed main(void) { int r,c; cin >> r >> c; int sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy; f(r+sx,c+sy,gx,gy); f(r+sx,c+sy,gx,c+gy); f(r+sx,c+sy,gx,c+c+gy); f(r+sx,c+sy,r+gx,gy); f(r+sx,c+sy,r+gx,c+gy); f(r+sx,c+sy,r+gx,c+c+gy); f(r+sx,c+sy,r+r+gx,gy); f(r+sx,c+sy,r+r+gx,c+gy); f(r+sx,c+sy,r+r+gx,c+c+gy); ncr[0][0] = 1; rep(i,MAX)rep(j,MAX){ if(i) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j]) % MOD; if(i && j) ncr[i][j] = (ncr[i][j] + ncr[i - 1][j - 1]) % MOD; } int ans = 0; rep(i,v.size()){ int x = v[i].first; int y = v[i].second; (ans += ncr[x+y][x]) %= MOD; } cout << ans << endl; }
#include<iostream> #include<algorithm> #define F 1001 using namespace std; int dp[F][F]; int main(){ for(int i=1;i<F;i++)for(int j=1;j<F;j++)dp[i][j]=0; for(int i=0;i<F;i++)dp[0][i]=1; for(int i=0;i<F;i++)dp[i][0]=1; for(int i=1;i<F;i++)for(int j=1;j<F;j++)dp[i][j]=(dp[i-1][j]+dp[i][j-1])%100000007; int r,e,a,b,c,d,ans; cin>>r>>e>>a>>b>>c>>d; if(a==c&&b==d){cout<<"1"<<endl;return 0;} a-=c,b-=d; if(a<0)a=-a;if(b<0)b=-b; if(a*2>r)a=r-a;if(b*2>e)b=e-b; ans=dp[a][b]; if(a*2==r)ans*=2;if(b*2==e)ans*=2; cout<<ans%100000007<<endl; return 0; }
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) #define MOD 100000007 using namespace std; typedef pair<int,int>P; int d[1000][1000],ans[1000][1000]; int dx[]{1,-1,0,0},dy[]{0,0,1,-1}; int main(){ int r,c,x1,y1,x2,y2;cin>>r>>c>>x1>>y1>>x2>>y2; queue<P>que; memset(d,-1,sizeof(d)); ans[x1][y1]=1;d[x1][y1]=0;que.push(P(x1,y1)); while(!que.empty()){ P p=que.front();que.pop(); rep(i,4){ int nx=(p.first+dx[i]+r)%r,ny=(p.second+dy[i]+c)%c; if(d[nx][ny]==-1){ d[nx][ny]=d[p.first][p.second]+1; que.push(P(nx,ny)); } if(d[nx][ny]==d[p.first][p.second]+1) (ans[nx][ny]+=ans[p.first][p.second])%=MOD; } } printf("%d\n",ans[x2][y2]); }
#include <cstdio> #include <algorithm> #define MOD (100000007) typedef long long lint; using namespace std; lint memo[1024][1024]; lint getWay(int x, int y) { if (x == 0 || y == 0){ return (1); } if (memo[x][y]){ return (memo[x][y]); } return (memo[x][y] = (getWay(x, y - 1) + getWay(x - 1, y)) % MOD); } int main() { int d[2], a[2], b[2]; int mul; scanf("%d %d %d %d %d %d", &d[0], &d[1], &a[0], &a[1], &b[0], &b[1]); mul = 1; for (int i = 0; i < 2; i++){ if (abs(a[i] - b[i]) == d[i] - abs(a[i] - b[i])){ mul *= 2; a[i] = abs(a[i] - b[i]); } else { a[i] = min(abs(a[i] - b[i]), d[i] - abs(a[i] - b[i])); } } printf("%lld\n", getWay(a[0], a[1]) * mul % MOD); return (0); }
#include<iostream> #include<vector> #include<cmath> using namespace std; int main(void){ int column,row,start_x,start_y,goal_x,goal_y,mod,vertical,horizontal,num,res,multi; mod=100000007; cin>>column>>row>>start_x>>start_y>>goal_x>>goal_y; vector<vector<int>> vec(row+column,vector<int>(row+column,0)); for(int i=0;i<row+column;i++){ for(int j=0;j<=i;j++){ if(j==0){ vec.at(i).at(0) = 1; continue; } else{ vec.at(i).at(j) = (vec.at(i-1).at(j) + vec.at(i-1).at(j-1))%mod; } } } multi=1; horizontal=abs(goal_x-start_x); vertical=abs(goal_y-start_y); if(start_x<=goal_x){ if(horizontal>abs(start_x+column-goal_x)){ horizontal = abs(start_x+column-goal_x); } else if(horizontal==abs(start_x+column-goal_x)){multi*=2; } } else{ if(horizontal>abs(goal_x+column-start_x)){ horizontal = abs(goal_x+column-start_x); } else if(horizontal==abs(goal_x+column-start_x)){multi*=2; } } if(start_y<=goal_y){ if(vertical>abs(start_y+row-goal_y)){ vertical = abs(start_y+row-goal_y); } else if(vertical==abs(start_y+row-goal_y)){ multi*=2; } } else{ if(vertical>abs(goal_y+row-start_y)){ vertical = abs(goal_y+row-start_y); } else if(vertical==abs(goal_y+row-start_y)){ multi*=2; } } res=(vec.at(horizontal+vertical).at(horizontal)*multi)%mod; cout<<res<<endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <queue> using namespace std; struct P{ int x, y; P(int x_, int y_){ x = x_; y = y_; } P(){} }; const int INF = 1e+8; int W, H, sx, sy, gx, gy, dist; int dx[4] = {0,0,-1,1}; int dy[4] = {-1,1,0,0}; int d[1001][1001], h[1001][1001], dp[1001][1001], f[1001][1001]; void bfs(){ for(int y=0 ; y < H ; y++ ){ for(int x=0 ; x < W ; x++ ){ d[y][x] = h[y][x] = INF; } } d[sy][sx] = 0; queue<P> q; q.push( P(sx,sy) ); while( !q.empty() ){ int x = q.front().x, y = q.front().y; q.pop(); for(int i=0 ; i < 4 ; i++ ){ int mx = (x + dx[i] + W) % W, my = (y + dy[i] + H) % H; if( d[y][x] + 1 < d[my][mx] ){ d[my][mx] = d[y][x] + 1; q.push( P(mx,my) ); } } } dist = d[gy][gx]; h[gy][gx] = 0; q.push( P(gx,gy) ); while( !q.empty() ){ int x = q.front().x, y = q.front().y; q.pop(); for(int i=0 ; i < 4 ; i++ ){ int mx = (x + dx[i] + W) % W, my = (y + dy[i] + H) % H; if( h[y][x] + 1 < h[my][mx] ){ h[my][mx] = h[y][x] + 1; q.push( P(mx,my) ); } } } } void solve(){ for(int y=0 ; y < H ; y++ ){ for(int x=0 ; x < W ; x++ ){ f[y][x] = d[y][x] + h[y][x]; d[y][x] = INF; dp[y][x] = 0; } } dp[sy][sx] = 1; d[sy][sx] = 0; queue<P> q; q.push( P(sx,sy) ); while( !q.empty() ){ int x = q.front().x, y = q.front().y; q.pop(); if( f[y][x] > dist ) continue; for(int i=0 ; i < 4 ; i++ ){ int mx = (x + dx[i] + W) % W, my = (y + dy[i] + H) % H; if( d[y][x] + 1 <= d[my][mx] && f[my][mx] == dist ){ dp[my][mx] = (dp[my][mx] + dp[y][x]) % 100000007; if( d[y][x] + 1 < d[my][mx] ){ d[my][mx] = d[y][x] + 1; q.push( P(mx,my) ); } } } } } int main(){ cin >> W >> H >> sx >> sy >> gx >> gy; bfs(); solve(); cout << dp[gy][gx] << endl; }
#include <iostream> #include <algorithm> #include <cstdlib> using namespace std; long r,c,a1,a2,b1,b2; long nCr[1001][1001]; int main() { cin>>r>>c>>a1>>a2>>b1>>b2; long w = min(abs(a1-b1), r-abs(a1-b1)); long h = min(abs(a2-b2), c-abs(a2-b2)); nCr[1][0] = nCr[1][1] = 1; for (int i = 2; i <= w+h; i++) { nCr[i][0] = 1; for (int j = 1; j <= w; j++) { if (i == j) { nCr[i][j] = 1; break; } nCr[i][j] = (nCr[i-1][j-1] + nCr[i-1][j]) % 100000007; } } long a = nCr[w+h][w]; if (r == w * 2) a *= 2; if (c == h * 2) a *= 2; cout<<a % 100000007<<endl; }
#include<bits/stdc++.h> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) using namespace std; typedef long long ll; const ll MOD = 100000007LL; const int IINF = INT_MAX; int h,w,ax,ay,bx,by; ll memo[1010][1010]; int mindist[1010][1010]; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; bool isValid(int x,int y) { return 0 <= x && x < w && 0 <= y && y < h; } int main(){ cin >> w >> h >> ax >> ay >> bx >> by; rep(i,h) rep(j,w) mindist[i][j] = IINF; mindist[ay][ax] = 0; memo[ay][ax] = 1LL; deque<int> deq; deq.push_back(ax+ay*w); while( !deq.empty() ){ int cur = deq.front(); deq.pop_front(); int x = cur % w, y = cur / w; rep(i,4){ int nx = x + dx[i], ny = y + dy[i]; if( !isValid(nx,ny) ) { if( nx < 0 ) nx = w - 1; if( nx >= w ) nx = 0; if( ny < 0 ) ny = h - 1; if( ny >= h ) ny = 0; } if( mindist[ny][nx] >= mindist[y][x] + 1 ) { if( mindist[ny][nx] > mindist[y][x] + 1 ) deq.push_back(nx+ny*w); mindist[ny][nx] = mindist[y][x] + 1; ( memo[ny][nx] += memo[y][x] ) %= MOD; } } } cout << memo[by][bx] << endl; return 0; }
#include<cstdio> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef long long ll; const ll M=100000007; const int N_BIN=3000; ll nCr[N_BIN+1][N_BIN+1]; void binom(){ rep(n,N_BIN+1) nCr[n][0]=1; rep(n,N_BIN) rep(r,n+1) nCr[n+1][r+1]=(nCr[n][r+1]+nCr[n][r])%M; } int main(){ binom(); int h,w,a1,a2,b1,b2; scanf("%d%d%d%d%d%d",&h,&w,&a1,&a2,&b1,&b2); int y=min(abs(b1-a1),h-abs(b1-a1)); int x=min(abs(b2-a2),w-abs(b2-a2)); ll coef=1; if(abs(b1-a1)==h-abs(b1-a1)) coef*=2; if(abs(b2-a2)==w-abs(b2-a2)) coef*=2; printf("%lld\n",coef*nCr[x+y][y]%M); return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i,k,n) for(int i = (k); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define ALL(a) begin(a),end(a) #define MS(m,v) memset(m,v,sizeof(m)) #define D10 fixed<<setprecision(5) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef complex<double> Point; typedef long long ll; const int INF = 114514810; const int MOD = 100000007; const double EPS = 1e-8; const double PI = acos(-1.0); struct edge { int from, to, cost; bool operator < (const edge& e) const { return cost < e.cost; } bool operator >(const edge& e) const { return cost > e.cost; } }; ///*************************************************************************************/// ///*************************************************************************************/// ///*************************************************************************************/// int com[1005][1005]; void init() { MS(com, 0); REP(i, 1005) com[i][0] = com[0][i] = 1; FOR(i, 1, 1005)FOR(j, 1, 1005) com[i][j] = (com[i][j - 1] + com[i - 1][j]) % MOD; } int main() { int r, c, a1, b1, a2, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; int d[4]; int x1 = abs(a1 - b1); int x2 = (a1 > b1 ? r - a1 + b1 : r - b1 + a1); int y1 = abs(a2 - b2); int y2 = (a2 > b2 ? c - a2 + b2 : c - b2 + a2); d[0] = x1 + y1; d[1] = x1 + y2; d[2] = x2 + y1; d[3] = x2 + y2; int cost = *min_element(ALL(d)); init(); int ans = 0; if (cost == d[0]) { ans += com[x1][y1]; } if (cost == d[1]) { ans += com[x1][y2]; } if (cost == d[2]) { ans += com[x2][y1]; } if (cost == d[3]) { ans += com[x2][y2]; } cout << ans%MOD << endl; return 0; }
#include <stdio.h> #include <algorithm> #define mod 100000007 using namespace std; int r, c, a1, a2, b1, b2, f1, f2, dp[1001][1001]; int main() { scanf("%d%d%d%d%d%d", &r, &c, &a1, &a2, &b1, &b2); if(a1 > b1) swap(a1, b1); if(a2 > b2) swap(a2, b2); f1 = ((b1 - a1) * 2 == r ? 2 : 1); f2 = ((b2 - a2) * 2 == c ? 2 : 1); if((b1 - a1) * 2 > r) a1 += r, swap(a1, b1); b1 -= a1, a1 = 0; if((b2 - a2) * 2 > c) a2 += c, swap(a2, b2); b2 -= a2, a2 = 0; dp[0][0] = 1; for(int i = 0; i <= 1000; i++) { for(int j = 0; j <= 1000; j++) { if(i) dp[i][j] = (dp[i][j] + dp[i - 1][j]) % mod; if(j) dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod; } } printf("%d\n", dp[b1][b2] * f1 * f2 % mod); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll mod=1e8+7; ll add(ll a,ll b){ return (a+b)%mod; } ll mul(ll a,ll b){ return (a*b)%mod; } ll mpow(ll a,ll b){ if(b==0)return 1; ll res=mpow( mul(a,a) , b/2); if(b&1)res=mul(res,a); return res; } ll divi(ll a,ll b){ return mul(a, mpow(b,mod-2) ); } ll fact[10000]; ll nCr(ll n,ll r){ return divi( fact[n] , mul(fact[r],fact[n-r])); } ll H,W,a,b,x,y; int main(){ fact[0]=1; for(int i=1;i<10000;i++)fact[i]=mul(fact[i-1],i); cin>>W>>H>>a>>b>>x>>y; ll dx=min( abs(a-x) , W - abs(a-x) ); ll dy=min( abs(b-y) , H - abs(b-y) ); ll ans=nCr(dx+dy,dx); if( abs(a-x)*2 == W ) ans=mul(ans,2); if( abs(b-y)*2 == H ) ans=mul(ans,2); cout<<ans<<endl; return 0; }
#include<iostream> using namespace std; int a, b, c, d, e, f; int dp[4000][4000]; int main() { dp[0][0] = 1; for (int i = 0; i < 4000; i++) { for (int j = 0; j < 4000; j++) { if (i == 0 || j == 0)dp[i][j] = 1; else { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } dp[i][j] %= 100000007; } } int n, m, a, b, c, d, p = 1000000000, q = 0; cin >> n >> m >> a >> b >> c >> d; for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { int x = c + i*n; int y = d + j*m; x -= a; y -= b; if (x < 0)x = -x; if (y < 0)y = -y; if (x + y < p) { p = x + y; q = dp[x][y]; } else if (x + y == p) { q += dp[x][y]; q %= 100000007; } } } cout << q << endl; return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <list> #include <cmath> #include <fstream> #include <algorithm> #include <string> #include <queue> #include <set> #include <map> #include <complex> #include <iterator> #include <cstdlib> #include <cstring> #include <sstream> #include <stack> #include <climits> #include <deque> #include <bitset> #include <cassert> #include <ctime> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int dy[]={-1,0,1,0},dx[]={0,1,0,-1}; // adjust problem by problem const double EPS=1e-8; const double PI=acos(-1.0); #ifdef __GNUC__ int popcount(int n){return __builtin_popcount(n);} int popcount(ll n){return __builtin_popcountll(n);} #endif #ifndef __GNUC__ template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;} #endif template<class T>int SIZE(T a){return a.size();} template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();} template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;} template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);} template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;} bool EQ(double a,double b){return abs(a-b)<EPS;} void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);} vector<string> split(string str,char del){ vector<string> res; for(int i=0,s=0;i<SIZE(str);i++){ if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;} else if(i==SIZE(str)-1){res.push_back(str.substr(s));} } return res; } int H,W; int sx,sy,gx,gy; const int mod=100000007; const int MAX_FACT=10000; ll fact[MAX_FACT]; ll pow_mod(ll a,ll n){ ll cur=1; ll mul=a; while(n){ if(n%2)cur=(cur*mul)%mod; mul=(mul*mul)%mod; n/=2; } return cur; } ll div_mod(int a){ return pow_mod(a,mod-2); } // factを前計算 void calcFact(){ fact[0]=1; for(int i=1;i<MAX_FACT;i++) fact[i]=(fact[i-1]*i)%mod; } // aCbのmodを計算 // 計算量はO(log(fact[a])+log(fact[a-b]))程度 ll comb(ll a,ll b){ if(a<0||b<0)return 0; else if(b==0)return 1; ll res=fact[a]; res=(res*div_mod(fact[b]))%mod; res=(res*div_mod(fact[a-b]))%mod; return res; } int main(){ calcFact(); cin>>H>>W>>sy>>sx>>gy>>gx; vector<pii> cand; ll res=0; cand.push_back(pii(gy,gx)); // left cand.push_back(pii(gy,-(W-gx))); // right cand.push_back(pii(gy,W+gx)); // up cand.push_back(pii(-(H-gy),gx)); // down cand.push_back(pii(H+gy,gx)); // left up cand.push_back(pii(-(H-gy),-(W-gx))); // right up cand.push_back(pii(-(H-gy),W+gx)); // left down cand.push_back(pii(H+gy,-(W-gx))); // right down cand.push_back(pii(H+gy,W+gx)); int minDist=1<<30; for(int i=0;i<9;i++) minDist=min(minDist,abs(sy-cand[i].first) +abs(sx-cand[i].second)); for(int i=0;i<9;i++){ int a=abs(sy-cand[i].first); int b=abs(sx-cand[i].second); if(minDist==(a+b)) res=(res+comb(a+b,a))%mod; } cout<<res<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long const int mod=100000007; int comb[1001][1001]; signed main(){ comb[0][0]=1; for(int i=1;i<=1000;i++){ comb[i][0]=comb[i][i]=1; for(int j=1;j<i;j++){ comb[i][j]=(comb[i-1][j]+comb[i-1][j-1])%mod; } } int r,c,a1,a2,b1,b2;cin>>r>>c>>a1>>a2>>b1>>b2; int wc=min(abs(a1-b1),r-abs(a1-b1)); int hc=min(abs(a2-b2),c-abs(a2-b2)); int ans=comb[wc+hc][hc]; if(abs(a1-b1)==r-abs(a1-b1))ans=ans*2%mod; if(abs(a2-b2)==c-abs(a2-b2))ans=ans*2%mod; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> //#define INF 100000000 //#define MOD (int) (1e9+7) #define rep(i, a) for (int i = 0; i < (a); i++) using namespace std; const int MAX = 1000000; const int MOD = 100000007; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } int main() { long long int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; long long int m = min(abs(b1 - a1), r - abs(b1 - a1)); long long int n = min(abs(b2 - a2), c - abs(b2 - a2)); // 前処理 COMinit(); long long int ans = COM(m+n, m); if (r == m*2) { ans = 2 * ans % MOD; } if (c == n*2) { ans = 2 * ans % MOD; } cout << ans << endl; }
#include <iostream> #include <algorithm> using namespace std; const int N = 2000; const int M = 100000007; int C[N+1][N+1]; void init(){ for(int i=0;i<N;i++){ C[i][0] = C[i][i] = 1; for(int j=0;j<i;j++) C[i][j] = (C[i-1][j-1] + C[i-1][j]) % M; } } int main(){ int r, c, sy, sx, ty, tx, by, bx; init(); int dy[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; cin >> r >> c >> sy >> sx >> ty >> tx; int ans = 0; by = abs(ty - sy); bx = abs(tx - sx); for(int i=0;i<9;i++){ int ny = abs(ty + r * dy[i] - sy); int nx = abs(tx + c * dx[i] - sx); if(by + bx > ny + nx){ by = ny; bx = nx; ans = C[by+bx][by]; }else if(by + bx == ny + nx){ ans = (ans + C[ny+nx][ny]) % M; } } cout << ans << endl; }
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<cstring> #include<queue> #include<cstdio> #include<sstream> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mp make_pair #define all(in) in.begin(),in.end() const double PI=acos(-1); const double EPS=1e-10; const int inf=1e9; using namespace std; typedef long long ll; typedef vector<ll> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; #define MOD 100000007 // a^b mod MOD ll powmod(ll a,ll b){ if(b==1)return a%MOD; ll out=powmod(a,b/2)%MOD; out=out*out%MOD; if(b%2)out=out*a%MOD; return out; } // nCr ll nCr(int n,int r){ ll out=1; r=min(r,n-r); for(int i=n;i>n-r;i--)out=out*i%MOD; for(int i=2;i<=r;i++)out=out*powmod(i,MOD-2)%MOD; return out; } int main(){ int n,m,a,b,c,d; cin>>n>>m>>a>>b>>c>>d; if(c<a)swap(a,c); if(d<b)swap(b,d); int x=min(c-a,n-c+a),y=min(d-b,m-d+b); ll out=nCr(x+y,y); if(c-a==n-c+a)out*=2; if(d-b==m-d+b)out*=2; cout<<out%MOD<<endl; }
#include <iostream> #include <cmath> #include <cstring> using namespace std; typedef unsigned long long ull; int dp[5001][5001]; int path(int x , int y){ ull ret; if(dp[x][y] != 0){ ret = dp[x][y]; } else { if(x == 0 || y == 0) ret = 1; else ret = path(x - 1, y) + path(x, y - 1); ret %= 100000007; dp[x][y] = ret; } return ret; } int mod(int n , int a){ int rtn = n%a; return n >= 0 ? rtn : (a + rtn)%a; } int main(){ memset(dp,0,sizeof(dp)); int r,c,a1,a2,b1,b2; cin >> r>>c>>a1>>a2>>b1>>b2; int dx1 = mod(a1 - b1, r); int dx2 = mod(-dx1, r); int dy1 = mod(a2 - b2, c); int dy2 = mod(-dy1, c); int dx = min(dx1,dx2); int dy = min(dy1,dy2); int rtn = path(dx , dy); if(dx1 == dx2 && a1 != b1) rtn = rtn * 2 % 100000007; if(dy1 == dy2 && a2 != b2) rtn = rtn * 2 % 100000007; cout << rtn << endl; return 0; }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int r,c,a1,a2,b1,b2; ll p=1; ll mod=100000007; ll dp[1001][1001]; int main(void){ scanf("%d%d%d%d%d%d",&r,&c,&a1,&a2,&b1,&b2); int dx=min(abs(a1-b1),r-abs(a1-b1)); int dy=min(abs(a2-b2),c-abs(a2-b2)); if(abs(a1-b1)==r-abs(a1-b1))p*=2; if(abs(a2-b2)==c-abs(a2-b2))p*=2; dp[0][0]=1; for(int i=0;i<=dy;i++){ for(int j=0;j<=dx;j++){ dp[j+1][i]+=dp[j][i]; if(dp[j+1][i]>=mod)dp[j+1][i]%=mod; dp[j][i+1]+=dp[j][i]; if(dp[j][i+1]>=mod)dp[j][i+1]%=mod; } } printf("%lld\n",dp[dx][dy]*p%mod); return 0; }
#include<iostream> #include<cmath> #include<algorithm> using namespace std; typedef long long int lli; lli pascal[1010][1010]; const int MOD=100000007; int main(){ pascal[0][0]=1; for(int i=1;i<1010;i++){ pascal[i][0]=pascal[i][i]=1; for(int j=1;j<i;j++) pascal[i][j]=(pascal[i-1][j] + pascal[i-1][j-1])%MOD; } int r,c,sx,sy,gx,gy; cin>>r>>c>>sx>>sy>>gx>>gy; int t=0; int xx,yy; xx=min(abs(sx-gx),r-abs(sx-gx)); yy=min(abs(sy-gy),c-abs(sy-gy)); if(r == abs(sx-gx)*2) t++; if(c == abs(sy-gy)*2) t++; cout << (pascal[xx+yy][xx]<<t)%MOD << endl; return 0; }
#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <queue> using namespace std; #define INF 1000000000 #define EPS 1e-9 #define PI acos(-1) typedef long long ll; typedef pair<ll, int> P; #define MAX_R 1005 #define MAX_C 1005 #define mod 100000007 int r, c; int a1, a2; int b1, b2; ll solve(int a, int b){ a = abs(a); b = abs(b); if(a == 0 || b == 0) return 1; ll dp[MAX_R+1][MAX_C+1]; memset(dp, 0, sizeof(dp)); for(int i = 0; i <= a; i++){ dp[i][0] = 1; } for(int i = 0; i <= b; i++){ dp[0][i] = 1; } for(int i = 1; i <= b; i++){ for(int j = 1; j <= a; j++){ dp[j][i] = (dp[j-1][i] + dp[j][i-1]) % mod; } } return dp[a][b] % mod; } int main(){ vector<P> d; cin >> r >> c >> a1 >> a2 >> b1 >> b2; int dx[] = {0,r,r,0,-r,-r,-r,0,r}; int dy[] = {0,0,c,c,c,0,-c,-c,-c}; for(int i = 0; i < 9; i++){ ll tmp = (a1+dx[i] - b1) * (a1+dx[i]-b1) + (a2+dy[i]-b2) * (a2+dy[i]-b2); d.push_back(P(tmp, i)); } sort(d.begin(), d.end()); ll ans = 0; ans += solve(b1 - (a1 + dx[d[0].second]), b2 - (a2 + dy[d[0].second])); for(int i = 1; i < 9; i++){ if(d[i-1].first != d[i].first) break; else ans = (ans + solve(b1 - (a1 + dx[d[i].second]), b2 - (a2 + dy[d[i].second]))) % mod; } cout << ans % mod << endl; return 0; }
#include <iostream> #include <queue> #define debug 0 using namespace std; int date[1000][1000]={0}; int cost[1000][1000]={0}; int r,c; int Px[4]={1,0,-1,0}; int Py[4]={0,-1,0,1}; int XX(int nowx,int P){ nowx+=Px[P]; if(nowx>=r)nowx=0; if(nowx<0)nowx=r-1; return nowx; } int YY(int nowy,int P){ nowy+=Py[P]; if(nowy>=c)nowy=0; if(nowy<0)nowy=c-1; return nowy; } int main(){ int ax,ay,bx,by,tx,ty; cin>>r>>c>>ax>>ay>>bx>>by; queue<int>x; queue<int>y; x.push(ax); y.push(ay); date[ax][ay]=1; cost[ax][ay]=1; while(x.empty()==0){ for(int i=0;i<4;i++){ tx=XX(x.front(),i); ty=YY(y.front(),i); if(date[tx][ty]==0){ date[tx][ty]=date[x.front()][y.front()]+1; cost[tx][ty]=cost[x.front()][y.front()]; x.push(tx); y.push(ty); } else if(date[tx][ty]==date[x.front()][y.front()]+1){ cost[tx][ty]=(cost[tx][ty]+cost[x.front()][y.front()])%100000007; } } x.pop(); y.pop(); } cout<<cost[bx][by]<<endl; if(debug){ for(int i=0;i<c;i++){ for(int j=0;j<r;j++)cout<<date[j][i]<<" "; cout<<endl; } } return 0; }
#include <stdio.h> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <vector> #include <iostream> #include <set> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 //#define MOD 1000000007 #define MOD 100000007 #define PRIME1 99999883 #define PRIME2 99999893 #define EPS 0.00000001 using namespace std; ll calc(ll x,ll y){ if(y == 0){ return x; }else{ return calc(y,x%y); } } int main(){ int R,C,a_row,a_col,b_row,b_col; scanf("%d %d %d %d %d %d",&R,&C,&a_row,&a_col,&b_row,&b_col); int TATE,YOKO; bool T_double = false,Y_double = false; if(a_row == b_row){ TATE = 0; }else{ if(a_row > b_row){ TATE = min(a_row-b_row,R-a_row+b_row); if(a_row-b_row == R-a_row+b_row)T_double = true; //??????????????£????????????????????£???????????????????????????2????????? }else{ TATE = min(b_row-a_row,R-b_row+a_row); if(b_row-a_row == R-b_row+a_row)T_double = true; } } if(a_col == b_col){ YOKO = 0; }else{ if(a_col > b_col){ YOKO = min(a_col-b_col,C-a_col+b_col); if(a_col-b_col == C-a_col+b_col)Y_double = true; }else{ YOKO = min(b_col-a_col,C-b_col+a_col); if(b_col-a_col == C-b_col+a_col)Y_double = true; } } ll bunshi[TATE+YOKO],bunbo[TATE+YOKO]; //??¢???????????§????????§ for(int i = 0; i < TATE+YOKO; i++){ bunshi[i] = i+1; } for(int i = 0; i < TATE; i++){ bunbo[i] = i+1; } for(int i = 0; i < YOKO; i++){ bunbo[TATE+i] = i+1; } ll common; for(int i = 0; i < TATE+YOKO;i++){ for(int k = 0; k < TATE+YOKO;k++){ if(bunshi[i] >= bunbo[k]){ common = calc(bunshi[i],bunbo[k]); }else{ common = calc(bunbo[k],bunshi[i]); } bunshi[i] /= common; bunbo[k] /= common; } } ll ans = 1; for(int i = 0; i < TATE+YOKO;i++){ ans *= bunshi[i]; ans %= MOD; } if(T_double){ ans *= 2; ans %= MOD; } if(Y_double){ ans *= 2; ans %= MOD; } //if(ans < 0)ans += MOD; printf("%lld\n",ans); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> using namespace std; #define MOD 100000007 typedef long long LL; int dp[2001][1001]; int combmod(int a, int b){ b = min(b, a - b); dp[0][0] = 1; for(int i = 1; i <= a; ++i){ dp[i][0] = 1; for(int j = 1; j <= b; ++j){ dp[i][j] = (dp[i-1][j-1] + dp[i-1][j]) % MOD; } } return dp[a][b]; } int main(){ int r, c, a1, a2, b1, b2; cin >> r >> c >> a1 >> a2 >> b1 >> b2; int dx = abs(a1 - b1), dy = abs(a2 - b2); LL u = 1; if( dx == r - dx ){ u *= 2; } else if( dx > r - dx ){ dx = r - dx; } if( dy == c - dy ){ u *= 2; } else if( dy > c - dy ){ dy = c - dy; } int t = (combmod(dx + dy, dx) * u) % MOD; cout << t << endl; }
#include<stdio.h> #include<stdlib.h> long long dp[1005][1005]; int min(int a,int b){return a<b?a:b;} long long dfs(int gx,int gy){ int i,j; for(i=0;i<=gx+1;i++)for(j=0;j<=gy+1;j++)dp[i][j]=0; dp[1][1]=1; for(i=1;i<=gx+1;i++)for(j=1;j<=gy+1;j++)if(i!=1||j!=1)dp[i][j]=(dp[i-1][j]+dp[i][j-1])%100000007; return dp[gx+1][gy+1]; } int main(){ int gx,gy; int r,c,sx,sy; int i; scanf("%d %d %d %d %d %d",&r,&c,&sx,&sy,&gx,&gy); int x=abs(gx-sx); int y=abs(gy-sy); x=min(x,r-x); y=min(y,c-y); long long ans=1; if(r%2==0&&r/2==x)ans*=2; if(c%2==0&&c/2==y)ans*=2; ans*=dfs(x,y); printf("%lld\n",ans%100000007); return 0; }
#include <iostream> #define REP(i,n) for(int i=0; i<(int)(n); i++) #include <queue> #include <cstdio> inline int getInt(){ int s; scanf("%d", &s); return s; } typedef long long ll; #include <set> using namespace std; ll dp[1000][1000]; ll d[1000][1000]; const int mod = 100000007; const int _dx[] = {0,1,0,-1}; const int _dy[] = {-1,0,1,0}; int main(){ const int w = getInt(); const int h = getInt(); const int sx = getInt(); const int sy = getInt(); const int gx = getInt(); const int gy = getInt(); queue<pair<int, int> > q; REP(i,h) REP(j,w){ dp[i][j] = 0; d[i][j] = -1; } d[sy][sx] = 0; dp[sy][sx] = 1; q.push(make_pair(sx, sy)); while(q.size()){ const int x = q.front().first; const int y = q.front().second; q.pop(); REP(i,4){ const int xx = (x + _dx[i] + w) % w; const int yy = (y + _dy[i] + h) % h; if(d[yy][xx] == -1){ d[yy][xx] = d[y][x] + 1; q.push(make_pair(xx, yy)); } if(d[yy][xx] == d[y][x] + 1){ dp[yy][xx] += dp[y][x]; dp[yy][xx] %= mod; } } } cout << dp[gy][gx] << endl; return 0; }