text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; #define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename Tuple> void print_tuple(ostream&,const Tuple&){} template<typename Car,typename... Cdr,typename Tuple> void print_tuple(ostream& os,const Tuple& t){ print_tuple<Cdr...>(os,t); os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t); } template<typename... Args> ostream& operator<<(ostream& os,const tuple<Args...>& t){ print_tuple<Args...>(os<<'(',t); return os<<')'; } template<typename Ch,typename Tr,typename C,typename=decltype(begin(C()))> basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){ os<<'['; for(auto i=begin(c);i!=end(c);++i) os<<(i==begin(c)?"":" ")<<*i; return os<<']'; } constexpr int INF=1e9; constexpr int MOD=1e9+7; constexpr double EPS=1e-9; bool GaussJordan(const vvd& _a,const vd& b,vd& x) { int n=_a.size(); vvd a(n,vd(n+1)); rep(i,n){ copy(all(_a[i]),begin(a[i])); a[i][n]=b[i]; } rep(i,n){ int p=i; repi(j,i+1,n) if(abs(a[p][i])<abs(a[j][i])) p=j; if(abs(a[p][i])<EPS) return false; swap(a[i],a[p]); peri(j,i,n+1) a[i][j]/=a[i][i]; rep(j,n) if(j!=i) peri(k,i,n+1) a[j][k]-=a[j][i]*a[i][k]; } rep(i,n) x[i]=a[i][n]; return true; } struct Edge{ int src,dst; double cost; int cap,flow; Edge(){} Edge(int s,int d,double co,int ca=0,int f=0):src(s),dst(d),cost(co),cap(ca),flow(f){} }; bool operator<(const Edge& a,const Edge& b){return a.cost<b.cost;} bool operator>(const Edge& a,const Edge& b){return a.cost>b.cost;} struct Graph{ vector<Edge> es; vi head,next; Graph(){} Graph(int n):head(n,-1){} // コスト:-co,容量:0の逆辺も追加する void AddEdge(int u,int v,double co,int ca){ es.emplace_back(u,v,co,ca); next.push_back(head[u]); head[u]=es.size()-1; es.emplace_back(v,u,-co,0); next.push_back(head[v]); head[v]=es.size()-1; } }; double MinCostFlow(Graph& g,int tap,int sink,int flow) { int n=g.head.size(); double res=0; vd pots(n); while(flow){ vd cost(n,INF); vi prev(n,-1); priority_queue<Edge,vector<Edge>,greater<Edge>> pq; pq.emplace(-1,tap,0); while(pq.size()){ Edge cur=pq.top(); pq.pop(); if(cur.cost>cost[cur.dst]-EPS) continue; cost[cur.dst]=cur.cost; prev[cur.dst]=cur.src; for(int i=g.head[cur.dst];i!=-1;i=g.next[i]){ Edge e=g.es[i]; if(e.cap-e.flow==0) continue; pq.emplace(i,e.dst,cost[e.src]+e.cost+pots[e.src]-pots[e.dst]); } } if(cost[sink]==INF) return -1; rep(i,n) pots[i]+=cost[i]; int augment=flow; for(int v=sink;v!=tap;v=g.es[prev[v]].src){ Edge e=g.es[prev[v]]; augment=min(augment,e.cap-e.flow); } if(augment<EPS) return -1; for(int v=sink;v!=tap;v=g.es[prev[v]].src){ int i=prev[v]; g.es[i].flow+=augment; g.es[i^1].flow-=augment; } flow-=augment; res+=augment*pots[sink]; } return res; } void solve() { int n,s,t,f; cin>>n>>s>>t>>f; vd cs(n); { vvd a(n,vd(n)); vd b(n); rep(i,n){ rep(j,n) cin>>a[i][j]; cin>>b[i]; } GaussJordan(a,b,cs); } Graph g(n); rep(i,n){ int m; cin>>m; vi ds(m),fs(m); rep(j,m) cin>>ds[j]; rep(j,m) cin>>fs[j]; rep(j,m) g.AddEdge(i,ds[j],abs(cs[ds[j]]-cs[i]),fs[j]); } double res=MinCostFlow(g,s,t,f); if(res==-1) puts("impossible"); else printf("%.10f\n",res); } int main() { int tc; cin>>tc; rep(_,tc) solve(); }
#include<bits/stdc++.h> using namespace std; #define MAX_V 105 #define INF 1000000001 typedef pair< double ,int> P; struct edge { int to; int cap; int rev; double cost; }; int V; vector<edge> G[MAX_V]; double h[MAX_V]; double dist[MAX_V]; int prevv[MAX_V],preve[MAX_V]; void init_edge(){ for(int i=0;i<MAX_V;i++)G[i].clear(); } void add_edge(int from,int to,int cap,double cost){ G[from].push_back((edge){to,cap,(int)G[to].size(),cost}); G[to].push_back((edge){from,0,(int)G[from].size()-1,-cost}); } double min_cost_flow(int s,int t,int f){ double eps = 1e-5; double res = 0; fill(h,h+V,0.0); while(f>0){ priority_queue< P, vector<P>, greater<P> > que; fill( dist, dist+V , (double)INF ); dist[s]=0; que.push(P(0,s)); // fill ( prevv , prevv + V , s ); while(!que.empty()){ P p = que.top(); que.pop(); int v = p.second; if(dist[v]+eps < p.first)continue; for(int i=0;i<(int)G[v].size();i++){ edge &e = G[v][i]; if(e.cap>0&&dist[e.to] > eps+dist[v]+e.cost+h[v]-h[e.to]){ dist[e.to]=dist[v]+e.cost+h[v]-h[e.to]; prevv[e.to]=v; preve[e.to]=i; que.push(P(dist[e.to],e.to)); } } } // if(dist[t] == INF){ if( dist[t] > INF - eps ){ return -1; } for(int v=0;v<V;v++)h[v]+=dist[v]; int d=f; for(int v=t;v!=s;v=prevv[v]){ d=min(d,G[prevv[v]][preve[v]].cap); } f-=d; res+=d*h[t]; for(int v=t;v!=s;v=prevv[v]){ edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } typedef vector< double > vec; typedef vector<vec> mat; vec gauss_jordan(const mat&A,const vec&b){ int n=A.size(); mat B(n,vec(n+1)); for(int i=0;i<n;i++) for(int j=0;j<n;j++)B[i][j]=A[i][j]; for(int i=0;i<n;i++)B[i][n]=b[i]; for(int i=0;i<n;i++){ int pivot=i; for(int j=i;j<n;j++) if(abs(B[j][i])>abs(B[pivot][i]))pivot=j; swap(B[i],B[pivot]); if(abs(B[i][i])< 0.0000001 )return vec(); for(int j=i+1;j<=n;j++)B[i][j]/=B[i][i]; for(int j=0;j<n;j++){ if(i!=j) for(int k=i+1;k<=n;k++)B[j][k]-=B[j][i]*B[i][k]; } } vec x(n); for(int i=0;i<n;i++)x[i]=B[i][n]; return x; } int N,si,ti,F; int M; int main(){ int T; cin>>T; while(T--){ init_edge(); cin>>N>>si>>ti>>F; mat A( N , vec(N) ); vec B( N ); for(int i=0;i<N;i++){ for(int j=0;j<N;j++)scanf("%lf",&A[i][j]); scanf("%lf",&B[i]); } vec C=gauss_jordan(A,B); for(int i=0;i<N;i++){ cin>>M; vector<int> d(M); vector<int> f(M); for(int j=0;j<M;j++)scanf("%d",&d[j]); for(int j=0;j<M;j++)scanf("%d",&f[j]); for(int j=0;j<M;j++){ add_edge(i,d[j],f[j], abs(C[i]-C[d[j]]) ); } } V=N; double ans=min_cost_flow(si,ti,F); if(ans<=-1.0){ cout<<"impossible"<<endl; }else{ printf("%.10f\n",ans); } } return 0; }
#include<iostream> using namespace std; const int M = 1000000; int n; int sch[400][20]; bool dp[2][M]; int dy[] = {0,-1,0,1,0}, dx[] = {0,0,1,0,-1}; bool table[5][5]; int y,x; int main(){ while(cin >> n,n){ for(int i=0;i<n;i++){ for(int j=0;j<16;j++)cin >> sch[i][j]; } for(int i=0;i<M;i++)dp[0][i] = false; dp[0][4] = !sch[0][5] && !sch[0][6] && !sch[0][9] && !sch[0][10]; for(int i=0;i<n-1;i++){ int now = i&1, nxt = 1-now; for(int j=0;j<M;j++)dp[nxt][j] = false; for(int j=0;j<M;j++){ if(dp[now][j]){ for(int ii=0;ii<4;ii++){ for(int jj=0;jj<4;jj++){ table[ii][jj] = ((i<5)?true:false); } } int tmp = j; for(int z=0;z<6;z++){ int pos = tmp%10; y = pos/3; x = pos%3; for(int ii=0;ii<=1;ii++){ for(int jj=0;jj<=1;jj++){ table[y+ii][x+jj] = true; } } tmp/=10; } tmp = j%10; y = tmp/3; x = tmp%3; for(int ii=1;ii<=2;ii++){ for(int jj=0;jj<=4;jj++){ bool f = true; int ny = y+ii*dy[jj], nx = x+ii*dx[jj]; if(ny<0 || nx<0 || ny>=3 || nx>=3)continue; bool piyo[5][5]; for(int iii=0;iii<4;iii++){ for(int jjj=0;jjj<4;jjj++){ piyo[iii][jjj] = table[iii][jjj]; } } for(int iii=0;iii<=1;iii++){ for(int jjj=0;jjj<=1;jjj++){ if(sch[i+1][ (ny+iii)*4+nx+jjj ]){ f = false; goto END; } piyo[ny+iii][nx+jjj] = true; } } for(int iii=0;iii<4;iii++){ for(int jjj=0;jjj<4;jjj++){ if(!piyo[iii][jjj]){ f = false; goto END; } } } END:; dp[nxt][(j*10+(ny*3+nx))%M] |= f; } } } } } bool ans = false; for(int i=0;i<M;i++)ans |= dp[(n-1)&1][i]; if(ans)cout << "1\n"; else cout << "0\n"; } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1243> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ map<int,int> mp; vector<vector<int>> nextS(9); void init(){ int masu[4][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, }; int idx = 0; for(int i = 0; i < 3;i++){ for(int j = 0; j < 3;j++){ int S = 0; S = (1<<masu[i][j]) + (1<<masu[i][j+1]) + (1<<masu[i+1][j]) + (1<<masu[i+1][j+1]); mp[S] = idx++; //cout << S << " "; } //cout << endl; } /* 51 102 204 816 1632 3264 13056 26112 52224 */ nextS[0] = vector<int>{51,102,204,816,13056}; nextS[1] = vector<int>{51,102,204,1632,26112}; nextS[2] = vector<int>{51,102,204,3264,52224}; nextS[3] = vector<int>{51,816,1632,3264,13056}; nextS[4] = vector<int>{102,816,1632,3264,26112}; nextS[5] = vector<int>{204,816,1632,3264,52224}; nextS[6] = vector<int>{51,816,13056,26112,52224}; nextS[7] = vector<int>{102,1632,13056,26112,52224}; nextS[8] = vector<int>{204,3264,13056,26112,52224}; } set<tuple<int,int,ll>> memo; bool dfs(int n,int S,ll clowdcnt,const vector<int>& state){ if(n == state.size()) return true; if(S & state[n]) return false; if(memo.find(tuple<int,int,ll>(n,S,clowdcnt))!=memo.end()) return false; memo.insert(tuple<int,int,ll>(n,S,clowdcnt)); for(int i = 0; i < 16; i++){ ll nonrain = (clowdcnt>>(i*3))&7; clowdcnt &= (~(7LL<<(i*3))); nonrain++; if((S>>i)&1) nonrain = 0; if(nonrain == 7) return false; clowdcnt |= (nonrain<<(i*3)); } int idx = mp[S]; for(auto next:nextS[idx]) if(dfs(n+1,next,clowdcnt,state))return true; return false; } bool solve(int N){ memo.clear(); vector<int> state(N); for(int i = 0; i < N;i++){ int B = 0; for(int j = 0; j < 16;j++){ int c; cin >> c; B += (c<<j); } state[i] = B; } int beginS = (1<<5) + (1<<6) + (1<<9) + (1<<10); return dfs(0,beginS,0,state); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); int N; while(cin >> N,N){ cout << solve(N) << endl; } return 0; }
#include <bits/stdc++.h> #define range(i, a, n) for(int (i) = (a); (i) < (n); (i)++) #define rep(i, n) for(int (i) = 0; (i) < (n); (i)++) using namespace std; using vi = vector<int>; using vvi = vector<vi>; int n; int memo[366][3][3][7][7][7][7]; int fes[366][4][4]; vi dx = { 1, 0,-1, 0, 0}; vi dy = { 0,-1, 0, 1, 0}; bool rec(int day, int y, int x, int a, int b, int c, int d){ if(a == 7 or b == 7 or c == 7 or d == 7) return false; if(day == n){ return true; } if(memo[day][y][x][a][b][c][d] != -1) return memo[day][y][x][a][b][c][d]; rep(i, 2){ rep(j, 2){ if(fes[day][y + i][x + j]){ return memo[day][y][x][a][b][c][d] = false; } } } bool ret = false; rep(i, 5){ range(j, 1, 3){ int ny = y + dy[i] * j; int nx = x + dx[i] * j; if(nx < 0 or 2 < nx or ny < 0 or 2 < ny) continue; int na = a + 1, nb = b + 1, nc = c + 1, nd = d + 1; if(ny == 0 and nx == 0) na = 0; if(ny == 0 and nx == 2) nb = 0; if(ny == 2 and nx == 0) nc = 0; if(ny == 2 and nx == 2) nd = 0; ret |= rec(day + 1, ny, nx, na, nb, nc, nd); } } return memo[day][y][x][a][b][c][d] = ret; } int main(void){ for(; cin >> n, n;){ rep(i, n){ rep(y, 4){ rep(x, 4){ cin >> fes[i][y][x]; } } } rep(i1, n) rep(i2, 3) rep(i3, 3) rep(i4, 7) rep(i5, 7) rep(i6, 7) rep(i7, 7) memo[i1][i2][i3][i4][i5][i6][i7] = -1; cout << rec(0, 1, 1, 1, 1, 1, 1) << endl; } return 0; }
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; const int N = 16; const int L = 6; const int MAX_A = 366; const int MAX_B = 9; const int MAX_C = 7 * 7 * 7 * 7; int D; bool v[MAX_A][4][4], dp[MAX_A][MAX_B][MAX_C]; int vtoi(int *h){ int res = 0; for(int i = 3; i >= 0; --i){ res *= 7; res += h[i]; } return res; } void itov(int c, int *h){ REP(i, 4){ h[i] = c % 7; c /= 7; } } int check(int d, int y, int x, int c){ int h[4]; itov(c, h); h[0] = (y == 0 && x == 0 ? 0 : h[0] + 1); h[1] = (y == 0 && x == 2 ? 0 : h[1] + 1); h[2] = (y == 2 && x == 0 ? 0 : h[2] + 1); h[3] = (y == 2 && x == 2 ? 0 : h[3] + 1); REP(i, 4) if(h[i] >= 7) return -1; REP(i, 2) REP(j, 2) if(v[d][y + i][x + j]) return -1; return vtoi(h); } int solve(){ if(v[0][1][1] || v[0][1][2] || v[0][2][1] || v[0][2][2]) return 0; memset(dp, 0, sizeof(dp)); dp[0][4][400] = 1; REP(a, D - 1){ REP(b, MAX_B){ REP(c, MAX_C){ if(!dp[a][b][c]) continue; int nc; if((nc = check(a + 1, b / 3, b % 3, c)) != -1) dp[a + 1][b][nc] = 1; for(int i = -1; i <= 1; ++i){ for(int j = -1; j <= 1; ++j){ if((i != 0 && j != 0) || (i == 0 && j == 0)) continue; int y = b / 3 + i, x = b % 3 + j; while(y >= 0 && y <= 2 && x >= 0 && x <= 2){ if((nc = check(a + 1, y, x, c)) != -1) dp[a + 1][y * 3 + x][nc] = 1; y += i; x += j; } } } } } } bool res = 0; REP(i, MAX_B) REP(j, MAX_C) if(dp[D - 1][i][j]) res = 1; return res; } int main() { while(cin >>D && D){ REP(i, D) REP(j, 4) REP(k, 4) cin >>v[i][j][k]; cout <<solve() <<endl; } return 0; }
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<cstring> #include<queue> #include<stack> #include<cstdio> #include<sstream> #include<iomanip> #include<assert.h> #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 all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) using namespace std; //kaewasuretyuui typedef long long ll; typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; //#define mt make_tuple //typedef tuple<int,string> tp; //typedef vector<tp> vt; const double PI=acos(-1); const double EPS=1e-7; const int inf=1e9; const ll INF=2e18; int dx[]={0,1,0,-1,0,2,0,-2,0}; int dy[]={1,0,-1,0,2,0,-2,0,0}; int f(vp in){ int a=0; for(int i=5;i>=0;i--){ a*=9; a+=in[i].first*3+in[i].second; } return a; } vp F(int a){ vp out; rep(i,6){ int b=a%9; out.pb(pii(b/3,b%3)); a/=9; } return out; } vi dp; int main(){ int n; while(cin>>n,n){ vvi in(n,vi(16)); rep(i,n)rep(j,16)cin>>in[i][j]; int N=531441; dp=vi(N); vp qwr(6,pii(1,1)); dp[f(qwr)]=1; rep(i,n){ vi ndp(N); rep(j,N)if(dp[j])rep(k,9){ if(i==0&&k!=8)continue; vp ka=F(j); pii now=ka[5]; now.first+=dx[k]; now.second+=dy[k]; if(now.first<0||now.first>=3||now.second<0||now.second>=3)continue; ka.pb(now); bool h=true; if(in[i][now.first*4+now.second])continue; if(in[i][now.first*4+now.second+1])continue; if(in[i][now.first*4+now.second+4])continue; if(in[i][now.first*4+now.second+5])continue; rep(l,16){ bool H=false; rep(x,7){ if(ka[x].first*4+ka[x].second==l)H=true; if(ka[x].first*4+ka[x].second+1==l)H=true; if(ka[x].first*4+ka[x].second+4==l)H=true; if(ka[x].first*4+ka[x].second+5==l)H=true; } if(!H)h=false; } ka.erase(ka.begin()); if(i<6||h)ndp[f(ka)]=true; } dp=ndp; // rep(j,N)if(dp[j]){ // vp tmp=F(j); // rep(k,tmp.size())cout<<tmp[k].first<<" "<<tmp[k].second<<endl; // cout<<endl; // } } bool h=false; rep(i,N)if(dp[i])h=true; cout<<h<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef pair< int, int > Pi; int N; bool xx[365][4][4]; int dp[3][3][365][7][7][7][7]; bool isin(int x, int y, int depth) { if(0 <= x && 0 <= y && x < 3 && y < 3) { for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { if(xx[depth][x + i][y + j]) return(false); } } return(true); } return(false); } bool rec(int x, int y, int depth, int lt, int lb, int rt, int rb) { if(x == 0 && y == 0) lt = 0; if(x == 0 && y == 2) lb = 0; if(x == 2 && y == 0) rt = 0; if(x == 2 && y == 2) rb = 0; if(lt > 6 || lb > 6 || rt > 6 || rb > 6) return(false); if(!isin(x, y, depth)) return(false); if(depth == N) return(true); auto& curr = dp[x][y][depth][lt][lb][rt][rb]; if(~curr) return(curr); for(int i = -2; i <= 2; i++) { if(rec(x + i, y, depth + 1, lt + 1, lb + 1, rt + 1, rb + 1)) return(curr = true); if(rec(x, y + i, depth + 1, lt + 1, lb + 1, rt + 1, rb + 1)) return(curr = true); } return(curr = false); } int main() { while(cin >> N, N) { memset(dp, -1, sizeof(dp)); for(int k = 0; k < N; k++) { for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { cin >> xx[k][i][j]; } } } cout << rec(1, 1, 0, 1, 1, 1, 1) << endl; } }
#include<iostream> #include<cassert> using namespace std; #define DMAX 370 #define SMAX 4096 #define SIZE 16 static const int nextP[9+2][5] = {{0, 1, 2, 4, 8}, {0, 1, 2, 5, 9}, {0, 1, 2, 6, 10}, {-1, -1, -1, -1 -1}, {0, 4, 5, 6, 8}, {1, 4, 5, 6, 9}, {2, 4, 5, 6, 10}, {-1, -1, -1, -1 -1}, {0, 4, 8, 9, 10}, {1, 5, 8, 9, 10}, {2, 6, 8, 9, 10}}; int nday; int schedule[DMAX][SIZE]; int no_rainy[SIZE]; bool visited[DMAX][11][SMAX]; bool checkFestival(int pos, int day ){ if ( schedule[day][pos + 0] == 1 || schedule[day][pos + 1] == 1 || schedule[day][pos + 4] == 1 || schedule[day][pos + 5] == 1 ) return true; return false; } bool check7NoRainy(){ for ( int i = 0; i < SIZE; i++ ){ if ( no_rainy[i] >= 7 ) return true; } return false; } int getState(){ return no_rainy[0] + no_rainy[3]*8 + no_rainy[12]*64 + no_rainy[15]*512; } bool recursive( int pos, int day ){ if ( day == nday ) return true; visited[day][pos][getState()] = true; int tmp[SIZE]; for ( int i = 0; i < SIZE; i++ ) tmp[i] = no_rainy[i]; for ( int n = 0; n < 5; n++ ){ if ( checkFestival( nextP[pos][n], day+1 ) ) continue; for ( int i = 0; i < SIZE; i++ ) no_rainy[i] = tmp[i]; for ( int i = 0; i < SIZE; i++ ){ if ( i == pos || i == pos + 1 || i == pos + 4 || i == pos + 5 ) { no_rainy[i] = 0; } else no_rainy[i]++; } if ( check7NoRainy() ) continue; if ( visited[day+1][nextP[pos][n]][getState()] ) continue; if ( recursive( nextP[pos][n], day+1 ) ) return true; } return false; } void compute(){ for ( int i = 0; i < SIZE; i++ ) no_rainy[i] = 0; for ( int i = 0; i < DMAX; i++ ){ for (int j = 0; j < 11; j++ ){ for ( int k = 0; k < SMAX; k++ ) visited[i][j][k] = false; } } if ( !checkFestival( 5, 0 ) && recursive( 5, 0 ) ) cout << 1 << endl; else cout << 0 << endl; } bool input(){ cin >> nday; if ( nday == 0 ) return false; for ( int i = 0; i < nday; i++ ){ for ( int j = 0; j < SIZE; j++ ) cin >> schedule[i][j]; } for ( int j = 0; j < SIZE; j++ ) schedule[nday][j] = 0; return true; } int main(){ while ( input() ) compute(); }
#include<iostream> #include<vector> #include<array> #include<algorithm> using namespace std; int n; int rd() { int ret=0; for(int i=0;i<16;i++) { int a;cin>>a; ret+=a<<i; } return ret; } vector<int>G[9]; int F[9]; int main() { G[0].push_back(0); G[0].push_back(1); G[0].push_back(2); G[0].push_back(3); G[0].push_back(6); G[1].push_back(0); G[1].push_back(1); G[1].push_back(2); G[1].push_back(4); G[1].push_back(7); G[2].push_back(0); G[2].push_back(1); G[2].push_back(2); G[2].push_back(5); G[2].push_back(8); G[3].push_back(0); G[3].push_back(3); G[3].push_back(4); G[3].push_back(5); G[3].push_back(6); G[4].push_back(1); G[4].push_back(3); G[4].push_back(4); G[4].push_back(5); G[4].push_back(7); G[5].push_back(2); G[5].push_back(3); G[5].push_back(4); G[5].push_back(5); G[5].push_back(8); G[6].push_back(0); G[6].push_back(3); G[6].push_back(6); G[6].push_back(7); G[6].push_back(8); G[7].push_back(1); G[7].push_back(4); G[7].push_back(6); G[7].push_back(7); G[7].push_back(8); G[8].push_back(2); G[8].push_back(5); G[8].push_back(6); G[8].push_back(7); G[8].push_back(8); F[0]=1<<0|1<<1|1<<4|1<<5; F[1]=1<<1|1<<2|1<<5|1<<6; F[2]=1<<2|1<<3|1<<6|1<<7; F[3]=1<<4|1<<5|1<<8|1<<9; F[4]=1<<5|1<<6|1<<9|1<<10; F[5]=1<<6|1<<7|1<<10|1<<11; F[6]=1<<8|1<<9|1<<12|1<<13; F[7]=1<<9|1<<10|1<<13|1<<14; F[8]=1<<10|1<<11|1<<14|1<<15; while(cin>>n,n) { vector<array<int,6> >now; int ret=rd(); if(!(ret&F[4])) { array<int,6>a; for(int i=0;i<5;i++)a[i]=-1; a[5]=4; now.push_back(a); } for(int i=1;i<n;i++) { ret=rd(); bool ok[9]={}; for(int j=0;j<9;j++)ok[j]=!(ret&F[j]); vector<array<int,6> >tmp; for(int j=0;j<now.size();j++) { array<int,6>a=now[j]; int out=(1<<16)-1; if(a[0]==-1)out=0; for(int k=0;k<6;k++)if(a[k]>=0)out&=~F[a[k]]; for(int k=0;k<5;k++) { int v=G[a[5]][k]; if(ok[v]&&!(out&~F[v])) { array<int,6>b; for(int l=0;l<5;l++)b[l]=a[l+1]; b[5]=v; tmp.push_back(b); } } } sort(tmp.begin(),tmp.end()); tmp.erase(unique(tmp.begin(),tmp.end()),tmp.end()); now=tmp; } cout<<!now.empty()<<endl; } }
#define _CRT_SECURE_NO_WARNINGS #pragma comment (linker, "/STACK:526000000") #include "bits/stdc++.h" using namespace std; typedef string::const_iterator State; #define eps 1e-11L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define seg_size 262144 * 4LL #define pb push_back #define mp make_pair typedef long long ll; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() void init() { iostream::sync_with_stdio(false); cout << fixed << setprecision(20); } //#define int ll unsigned long xor128() { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } int dp[9][7][7][7][7][500] = {}; void solve() { while (true) { int n; cin >> n; if (n == 0) return; REP(tea,9) REP(q, 7) { REP(t, 7) { REP(j, 7) { REP(p, 7) { REP(i,n+1) dp[tea][q][t][j][p][i] = 0; } } } } dp[4][1][1][1][1][0] = 1; const int dx[4] = { 1,0,-1,0 }; const int dy[4] = { 0,1,0,-1 }; REP(i, n) { vector<vector<int>> go; REP(q, 4) { go.push_back(vector<int>{}); REP(j, 4) { int a; cin >> a; go.back().push_back(a); } } REP(q, 9) { int x = q / 3; int y = q % 3; int die = 0; REP(t, 4) { int nx = x + dx[t / 2]; int ny = y + dy[t % 2]; if (go[nx][ny] == 1) { die = 1; } } if (die == 0) continue; int tea = q; REP(q, 7) { REP(t, 7) { REP(j, 7) { REP(p, 7) { dp[tea][q][t][j][p][i] = 0; } } } } } REP(x, 3) { REP(y, 3) { REP(q, 7) { REP(t, 7) { REP(j, 7) { REP(p, 7) { if (dp[x * 3 + y][q][t][j][p][i] == 0) continue; REP(dir, 4) { REP(cos, 4) { int new_x = x + dx[dir] * cos; int new_y = y + dy[dir] * cos; if (new_x >= 0 && new_x <= 2 && new_y >= 0 && new_y <= 2) { int nq = q + 1; int nt = t + 1; int nj = j + 1; int np = p + 1; if (new_x == 0 && new_y == 0) { nq = 0; } if (new_x == 2 && new_y == 0) { nt = 0; } if (new_x == 0 && new_y == 2) { nj = 0; } if (new_x == 2 && new_y == 2) { np = 0; } if (nq < 7 && nj < 7 && nt < 7 && np < 7) { dp[new_x * 3 + new_y][nq][nt][nj][np][i + 1] = 1; } } else break; } } } } } } } } } int ok = 0; REP(i,9) REP(q, 7) { REP(t, 7) { REP(j, 7) { REP(p, 7) { ok = max(ok, dp[i][q][t][j][p][n]); } } } } cout << ok << endl; } } #undef int int main() { init(); solve(); }
#include<set> #include<cstdio> #include<vector> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int to[9][5]; // to[i] : 雲が位置 i から移動できる位置 int n; int sun[366][16]; // sun[t][j]==1 <=> t 日目にマス j は晴れていないといけない int rain[9][16]; // rain[i][j]==1 <=> 雲が位置 i にいるとき, マス j は雨 set< vector<char> > vis[366][9]; bool dfs(int t,int i,vector<char> last){ if(t%2==0){ // MLE したので半分だけメモ化 if(vis[t][i].count(last)==1) return false; vis[t][i].insert(last); } rep(j,16){ if(rain[i][j]==1){ last[j]=0; if(sun[t][j]==1) return false; } else{ last[j]++; if(last[j]>=7) return false; } } if(t==n) return true; rep(j,5) if(dfs(t+1,to[i][j],last)) return true; return false; } int main(){ to[0][0]=0; to[0][1]=1; to[0][2]=2; to[0][3]=3; to[0][4]=6; to[1][0]=1; to[1][1]=0; to[1][2]=2; to[1][3]=4; to[1][4]=7; to[2][0]=2; to[2][1]=0; to[2][2]=1; to[2][3]=5; to[2][4]=8; to[3][0]=3; to[3][1]=0; to[3][2]=4; to[3][3]=5; to[3][4]=6; to[4][0]=4; to[4][1]=1; to[4][2]=3; to[4][3]=5; to[4][4]=7; to[5][0]=5; to[5][1]=2; to[5][2]=3; to[5][3]=4; to[5][4]=8; to[6][0]=6; to[6][1]=0; to[6][2]=3; to[6][3]=7; to[6][4]=8; to[7][0]=7; to[7][1]=1; to[7][2]=4; to[7][3]=6; to[7][4]=8; to[8][0]=8; to[8][1]=2; to[8][2]=5; to[8][3]=6; to[8][4]=7; rain[0][ 0]=rain[0][ 1]=rain[0][ 4]=rain[0][ 5]=1; rain[1][ 1]=rain[1][ 2]=rain[1][ 5]=rain[1][ 6]=1; rain[2][ 2]=rain[2][ 3]=rain[2][ 6]=rain[2][ 7]=1; rain[3][ 4]=rain[3][ 5]=rain[3][ 8]=rain[3][ 9]=1; rain[4][ 5]=rain[4][ 6]=rain[4][ 9]=rain[4][10]=1; rain[5][ 6]=rain[5][ 7]=rain[5][10]=rain[5][11]=1; rain[6][ 8]=rain[6][ 9]=rain[6][12]=rain[6][13]=1; rain[7][ 9]=rain[7][10]=rain[7][13]=rain[7][14]=1; rain[8][10]=rain[8][11]=rain[8][14]=rain[8][15]=1; while(scanf("%d",&n),n){ rep(t,n) rep(j,16) scanf("%d",sun[t]+j); rep(t,n+1) rep(i,9) vis[t][i].clear(); vector<char> last(16); // 最後に雨が降ったのは何日前か ( int だと MLE した ) puts(dfs(0,4,last)?"1":"0"); } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <map> #include <queue> #include <set> using namespace std; typedef long long ll; int n; //// Šeó‘Ô‚©‚çƒS[ƒ‹‚Ö‚½‚ǂ蒅‚¯‚é‚©‚Ç‚¤‚©‚ðƒƒ‚ƒ‰ƒCƒY //map<ll,bool> dp[3][3]; // // ó‘Ôs‚Ìidx”Ô–Ú‚Ì’l‚ðŽæ‚èo‚· int getValue(ll s,int idx){ int b1=((s>>idx*3)&1); int b2=((s>>idx*3+1)&1); int b3=((s>>idx*3+2)&1); return b1+2*b2+4*b3; } // ó‘Ôs‚Ìidx”Ô–Ú‚É’lx‚ðƒZƒbƒg void setValue(ll &s,int idx,int x){ int b1=((x>>0)&1); int b2=((x>>1)&1); int b3=((x>>2)&1); if(b1==0) s&=~(1LL<<idx*3); else s|=(1LL<<idx*3); if(b2==0) s&=~(1LL<<(idx*3+1)); else s|=(1LL<<(idx*3+1)); if(b3==0) s&=~(1LL<<(idx*3+2)); else s|=(1LL<<(idx*3+2)); } // // //// place‚͉_‚̍¶ã‚̈ʒu //bool dfs(ll s,int cy,int cx){ // if(dp[cy][cx].find(s)!=dp[cy][cx].end()) // return dp[cy][cx][s]; // // Še•ûŒü‚ÖˆÚ“® // for(int i = 0; i < 9; i++){ // int ny=cy+dy[i]; // int nx=cx+dx[i]; // // ‚͂ݏo‚È‚¢ê‡ // if(ny>=0&&nx>=0&&ny<3&&nx<3){ // bool res=(dfs(s,ny,nx)); // if(res)return dp[cy][cx][s]=res; // } // } // return dp[cy][cx][s]=false; //} const int dy[]={-2,-1,0,1,2,0,0,0,0}; const int dx[]={0,0,0,0,0,-1,-2,1,2}; typedef pair<ll,pair<int,int> > P; int field[370][4][4]; void solve(){ queue<P> *prv=new queue<P>(); queue<P> *nxt=new queue<P>(); while(cin>>n&&n!=0){ for(int i = 0; i < n; i++) for(int j = 0; j < 4; j++) for(int k = 0; k < 4; k++) cin>>field[i][j][k]; int idx=0; bool ok=false; bool fin=false; // ‰ŠúˆÊ’u‚ª‘Ó–‚©ƒ`ƒFƒbƒN for(int i = 1; i < 3; i++){ for(int j = 1; j <3; j++){ if(field[idx][i][j]){ fin=true; break; } } if(fin)break; } idx++; ll init=0; for(int i = 0; i < 4; i++){ for(int j = 0;j < 4; j++){ if(i==0||j==0||i==3||j==3) setValue(init,i*4+j,6); else setValue(init,i*4+j,7); } } prv->push(make_pair(init,make_pair(1,1))); while(!fin&&prv->size()){ set<ll> passed[10]; while(prv->size()){ ll cs=prv->front().first; int cy=prv->front().second.first; int cx=prv->front().second.second; prv->pop(); // Še•ûŒü‚ÖˆÚ“® for(int i = 0; i < 9; i++){ ll ns=cs; int ny=cy+dy[i]; int nx=cx+dx[i]; // ‚͂ݏo‚È‚¢‚©‚ˆړ®æ‚ɍՂ肪‘¶Ý‚µ‚È‚¢ if(ny>=0&&nx>=0&&ny<3&&nx<3){ bool moveOK=true; // ˆÚ“®æ‚ɍՂ肪‘¶Ý‚µ‚È‚¢‚Ì‚ªˆÚ“®‚ÌðŒ for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ if(field[idx][ny+j][nx+k]){ moveOK=false; break; } } if(!moveOK)break; } if(!moveOK)continue; // ‚±‚±‚ňړ®ˆ— // ‚»‚ꂼ‚ê‚̏ꏊ‚̐”Žš‚ð1‚‚ւ炷 for(int j = 0; j < 4; j++) for(int k = 0; k < 4; k++) setValue(ns,j*4+k,getValue(ns,j*4+k)-1); // ¡‰ñ‚͈̔͂ð7‚É–ß‚· for(int j = ny; j < ny+2; j++) for(int k = nx; k < nx+2; k++) setValue(ns,j*4+k,7); // zeroƒ`ƒFƒbƒN for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if(getValue(ns,j*4+k)==0){ moveOK=false; break; } } if(!moveOK)break; } if(!moveOK)continue; if(passed[ny*3+nx].find(ns)==passed[ny*3+nx].end()){ nxt->push(make_pair(ns,make_pair(ny,nx))); passed[ny*3+nx].insert(ns); } } } } swap(prv,nxt); idx++; if(idx==n){ if(prv->size()>=1){ while(prv->size())prv->pop(); ok=true; } break; } } if(ok)cout<<1<<endl; else cout<<0<<endl; } delete prv; delete nxt; } int main(){ solve(); return 0; }
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; template<class T> using Table = vector<vector<T>>; const int dx[4] = { -1,0,1,0 }; const int dy[4] = { 0,1,0,-1 }; const int dx2[4] = { 0,0,1,1 }; const int dy2[4] = { 0,1,1,0 }; struct field { vector<vector<int>>drys; int cy; int cx; long long int hash; field(vector<vector<int>>drys_, const int cy_, const int cx_) :drys(drys_), cy(cy_), cx(cx_),hash(0) { for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { hash = hash * 10 + drys[i][j]; } } hash = hash * 10 + cy; hash = hash * 10 + cx; } }; bool operator<(const field&l, const field&r) { return l.hash < r.hash; } int main() { while (1) { int N; cin >> N; if (!N)break; vector<vector<vector<int>>>fests(N, vector<vector<int>>(4, vector<int>(4))); for (int day = 0; day < N; ++day) { for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { int a; cin >> a; fests[day][y][x] = a; } } } set<field,less<field>,allocator<field>>fields; fields.emplace(field(vector<vector<int>>(4, vector<int>(4)), 1, 1)); for (int day = 0; day < N; ++day) { set<field>nextfields; for (const auto f : fields) { const int nowcx = f.cx; const int nowcy = f.cy; if (day) { for (int cway = 0; cway <4; ++cway) { for (int dis = 1; dis <= 2; ++dis) { vector<vector<int>>nowdrys(f.drys); for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { nowdrys[y][x]++; } } const int nexcx = nowcx + dx[cway] * dis; const int nexcy = nowcy + dy[cway] * dis; if (nexcx < 0 || nexcx >= 3 || nexcy < 0 || nexcy >= 3)continue; bool ok = true; for (int rainway = 0; rainway < 4; ++rainway) { const int rainy = nexcy + dy2[rainway]; const int rainx = nexcx + dx2[rainway]; if (fests[day][rainy][rainx])ok = false; nowdrys[rainy][rainx] = 0; } for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { if (nowdrys[y][x] >= 7)ok = false; } } if (ok) { nextfields.emplace(nowdrys, nexcy, nexcx); } } } } { const int nexcx = nowcx; const int nexcy = nowcy; vector<vector<int>>nowdrys(f.drys); for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { nowdrys[y][x]++; } } if (nexcx < 0 || nexcx>3 || nexcy < 0 || nexcy>3)continue; bool ok = true; for (int rainway = 0; rainway < 4; ++rainway) { const int rainy = nexcy + dy2[rainway]; const int rainx = nexcx + dx2[rainway]; if (fests[day][rainy][rainx])ok = false; nowdrys[rainy][rainx] = 0; } for (int y = 0; y < 4; ++y) { for (int x = 0; x < 4; ++x) { if (nowdrys[y][x] >= 7)ok = false; } } if (ok) { nextfields.emplace(nowdrys, nexcy, nexcx); } } } fields = nextfields; } if (!fields.empty())cout << 1 << endl; else cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX_N 366 #define MAX_C 9 #define MAX_R 2402 struct State{ int d,c,r[4]; State(int d,int c,int r[4]) : d(d),c(c) { for(int i = 0 ; i < 4 ; i++){ this->r[i] = r[i]; } } }; int N,arr[MAX_N]; bool visited[MAX_N][MAX_C][MAX_R]; const int d[5] = {-1,-3,1,3,0}; int next(int c,int dir){ if(c%3 == 0 && dir == 0){ return -1; } if(c/3 == 0 && dir == 1){ return -1; } if(c%3 == 2 && dir == 2){ return -1; } if(c/3 == 2 && dir == 3){ return -1; } return c + d[dir]; } bool check(int d,int c){ c += c/3; for(int i = 0 ; i < 16 ; i++){ if((arr[d] >> i & 1) && i == c){ return true; } if((arr[d] >> i & 1) && i == c+1){ return true; } if((arr[d] >> i & 1) && i == c+4){ return true; } if((arr[d] >> i & 1) && i == c+5){ return true; } } return false; } void calc(int c,int *r){ if(c == 0){ r[0] = 0; }else{ r[0]++; } if(c == 2){ r[1] = 0; }else{ r[1]++; } if(c == 6){ r[2] = 0; }else{ r[2]++; } if(c == 8){ r[3] = 0; }else{ r[3]++; } } int getValue(int *r){ int res = 0; for(int i = 0 ; i < 4 ; i++){ res *= 7; res += r[i]; } return res; } int solve(){ int r[4] = {0}; calc(4,r); memset(visited,false,sizeof(visited)); visited[0][4][getValue(r)] = true; queue<State> Q; Q.push(State(0,4,r)); while(!Q.empty()){ State s = Q.front(); Q.pop(); int day = s.d,cloud = s.c; if(check(day,cloud)){ continue; } if(day == N-1){ return 1; } for(int i = 0 ; i < 5 ; i++){ int cc = cloud,nr[4]; for(int j = 0 ; j < 2 ; j++){ int nc = next(cc,i); if(nc == -1){ break; } memcpy(nr,s.r,sizeof(nr)); calc(nc,nr); if(*max_element(nr,nr+4) >= 7){ cc = nc; continue; } int value = getValue(nr); if(!visited[day+1][nc][value]){ visited[day+1][nc][value] = true; Q.push(State(day+1,nc,nr)); } cc = nc; if(i == 4){ break; } } } } return 0; } int main(){ while(cin >> N,N){ for(int i = 0 ; i < N ; i++){ int x,bit = 0; for(int j = 0 ; j < 16 ; j++){ cin >> x; if(x == 1){ bit |= (1<<j); } } arr[i] = bit; } cout << solve() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define all(v) begin(v), end(v) #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++) const int inf = 1LL << 55; const int mod = 1e9 + 7; int n; int bit[400]; int dx[] = {0, 0, 1, 0, -1, 0, 2, 0, -2}; int dy[] = {0, 1, 0, -1, 0, 2, 0, -2, 0}; map<pair<int, vector<int> >, bool> mp; bool in(int x, int y) { return 0 <= x && x < 3 && 0 <= y && y < 3; } bool dfs(int day, vector<int> v) { if(mp.count(make_pair(day, v))) return mp[make_pair(day, v)]; if(day == n) return true; int x = v[0]%3, y = v[0]/3; for(int i = 0; i < 9; i++) { vector<int> nv(7); int tmp = 0; int nx = x + dx[i], ny = y + dy[i]; if(!in(nx, ny)) continue; tmp = 1<<(ny*4+nx) | 1<<((ny+1)*4+nx) | 1<<(ny*4+nx+1) | 1<<((ny+1)*4+nx+1); if(bit[day+1] & tmp) continue; int a[16] = {0}; for(int i = 0; i < 6; i++) nv[i+1] = v[i]; nv[0] = ny*3 + nx; if(day >= 5) { for(int j = 0; j < 7; j++) { int tx = nv[j]%3, ty = nv[j]/3; a[ty*4+tx]++; a[(ty+1)*4+tx]++; a[ty*4+tx+1]++; a[(ty+1)*4+tx+1]++; } bool flag = true; for(int j = 0; j < 16; j++) { flag &= (a[j] != 0); } if(!flag) continue; } if(dfs(day+1, nv)) return mp[make_pair(day, v)] = true; } return mp[make_pair(day, v)] = false; } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); while(cin >> n, n) { mp.clear(); for(int i = 0; i < n; i++) { bit[i] = 0; for(int j = 0; j < 16; j++) { int b; cin >> b; bit[i] |= b<<j; } } int tmp = 0; tmp = 1<<5 | 1<<6 | 1<<9 | 1<<10; if(bit[0] & tmp) { cout << 0 << endl; continue; } vector<int> v(7, -1); v[0] = 4; cout << (int)dfs(0, v) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, m, n) for (int i = (m); i < (n); i++) #define int long long bool dp[400][10][7][7][7][7]; bool wed[400][17]; int dx[4] = {0, 0, 1, 1}, dy[4] = {0, 1, 0, 1}; int dxx[9] = {-2, -1, 0, 1, 2, 0, 0, 0, 0}, dyy[9] = {0, 0, 0, 0, 0, -2, -1, 1, 2}; int pos[9] = {0, 1, 2, 4, 5, 6, 8, 9, 10}; bool solve() { int N; cin >> N; if (N == 0) return false; REP(i, N + 1) { REP(j, 10) REP(a, 7) REP(b, 7) REP(c, 7) REP(d, 7) { dp[i][j][a][b][c][d] = false; } } REP(i, N) { REP(j, 16) { cin >> wed[i][j]; } } // REP(i, N) { // REP(j, 16) { cout << wed[i][j] << " "; } // cout << endl; // } dp[0][4][1][1][1][1] = true; REP(k, 4) { if (wed[0][pos[4] + dx[k] * 4 + dy[k]]) dp[0][4][1][1][1][1] = false; } REP(i, N - 1) { REP(j, 9) { REP(a, 7) REP(b, 7) REP(c, 7) REP(d, 7) { if (!dp[i][j][a][b][c][d]) continue; int x = j / 3, y = j % 3; REP(k, 9) { int nx = x + dxx[k], ny = y + dyy[k]; int nj = nx * 3 + ny; if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; int np = pos[nj]; assert(np + 5 < 16); if (wed[i + 1][np] || wed[i + 1][np + 1] || wed[i + 1][np + 4] || wed[i + 1][np + 4 + 1]) continue; int na = a + 1, nb = b + 1, nc = c + 1, nd = d + 1; if (np == 0) na = 0; if (np == 2) nb = 0; if (np == 8) nc = 0; if (np == 10) nd = 0; if (na >= 7 || nb >= 7 || nc >= 7 || nd >= 7) continue; dp[i + 1][nj][na][nb][nc][nd] = true; } } } } bool ok = false; REP(j, 9) REP(a, 7) REP(b, 7) REP(c, 7) REP(d, 7) { if (dp[N - 1][j][a][b][c][d]) ok = true; } if (ok) cout << 1 << endl; else cout << 0 << endl; return true; } signed main() { while (solve()) ; // solve(); }
#include <vector> #include <list> #include <map> #include <set> #include <stack> #include <queue> #include <deque> #include <algorithm> #include <utility> #include <functional> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <climits> #include <cassert> #include <memory> #include <time.h> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define EXIST(s,e) ((s).find(e)!=(s).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) const double EPS = 1e-9; const double PI = acos(-1.0); typedef vector<vvi> vvvi; bool satisfied(vvi &field,int y,int x){ FOR(i,y,y+2){ FOR(j,x,x+2){ if(field[i][j]){ return false; } } } return true; } int h(int y,int x){ return y*3+x; } bool thirsty(vi pos){ vvi wetness(4,vi(4)); REP(i,pos.size()){ int y=pos[i]/3; int x=pos[i]%3; FOR(j,y,y+2){ FOR(k,x,x+2){ wetness[j][k]++; } } } REP(i,4){ REP(j,4){ if(wetness[i][j]==0){ return true; } } } return false; } int dp[2][9][9][9][9][9][9]; int main(){ int N; while(cin>>N,N){ REP(day,2)REP(i,9)REP(j,9)REP(k,9)REP(l,9)REP(m,9)REP(n,9)dp[day][i][j][k][l][m][n]=0; int d=0; REP(day,N){ vvi field(4,vi(4)); REP(i,4){ REP(j,4){ cin>>field[i][j]; } } REP(i,9)REP(j,9)REP(k,9)REP(l,9)REP(m,9)REP(n,9)dp[d][i][j][k][l][m][n]=0; if(day==0){ dp[d][0][0][0][0][0][h(1,1)]=satisfied(field,1,1); }else{ REP(i,9){ REP(j,9){ REP(k,9){ REP(l,9){ REP(m,9){ REP(n,9){ if(dp[(d+1)%2][i][j][k][l][m][n]){ int py=n/3,px=n%3; REP(o,9){ int cy=o/3,cx=o%3; if(cy==py||cx==px){ if(day>=6){ vi pos; pos.push_back(i); pos.push_back(j); pos.push_back(k); pos.push_back(l); pos.push_back(m); pos.push_back(n); pos.push_back(o); if(thirsty(pos))continue; } dp[d][j][k][l][m][n][o]|=satisfied(field,cy,cx); } } } } } } } } } } d=(d+1)%2; } bool ok=false; REP(i,9){ REP(j,9){ REP(k,9){ REP(l,9){ REP(m,9){ REP(n,9){ ok|=dp[(d+1)%2][i][j][k][l][m][n]; } } } } } } cout<<ok<<endl; } }
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; struct info{ int n,w,s,e,p; info(int n, int w, int s, int e, int p):n(n), w(w), s(s), e(e), p(p){} info(){} void decrease(){ n--; w--; s--; e--; } bool isvalid(){ return n>0 && w>0 && s>0 && e>0; } bool operator < (const info &a) const{ return (n +(w<<3) +(s<<6) +(e<<9) +(p<<12)) < (a.n +(a.w<<3) +(a.s<<6) +(a.e<<9) +(a.p<<12)); } }; int main(){ while(1){ int n; cin >> n; if(n==0) break; vector<vector<int> > w(n, vector<int>(17)); for(int i=0; i<n; i++){ for(int j=1; j<=16; j++){ cin >> w[i][j]; } } set<info> s; if(w[0][6] +w[0][7] +w[0][10] +w[0][11] == 0){ s.insert(info(6, 6, 6, 6, 6)); } for(int i=1; i<n; i++){ set<info> ns; for(set<info>::iterator itr=s.begin(); itr != s.end(); itr++){ info e = *itr; int x = (e.p -1) %4; int y = (e.p -1) /4; e.decrease(); for(int d=0; d<4; d++){ for(int t=(d==0)?0:1; t<=2; t++){ int nx = x +t*dx[d]; int ny = y +t*dy[d]; info ni = e; ni.p = 4*ny +nx +1; if(nx<0 || nx>2 || ny<0 || ny>2) break; if(w[i][ni.p] +w[i][ni.p+1] +w[i][ni.p+4] +w[i][ni.p+5] > 0) continue; if(ni.p == 1) ni.n = 7; if(ni.p == 3) ni.w = 7; if(ni.p == 9) ni.s = 7; if(ni.p == 11) ni.e = 7; if(ni.isvalid()) ns.insert(ni); } } } s = ns; } if(s.size()!=0){ cout << 1 << endl; }else{ cout << 0 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX_N 366 #define MAX_C 9 #define MAX_R 2402 struct State{ int d,c,r[4]; State(int d,int c,int r[4]) : d(d),c(c) { for(int i = 0 ; i < 4 ; i++){ this->r[i] = r[i]; } } }; int N,arr[MAX_N]; bool visited[MAX_N][MAX_C][MAX_R]; const int d[5] = {-1,-3,1,3,0}; int next(int c,int dir){ if(c%3 == 0 && dir == 0) return -1; if(c/3 == 0 && dir == 1) return -1; if(c%3 == 2 && dir == 2) return -1; if(c/3 == 2 && dir == 3) return -1; return c + d[dir]; } bool check(int d,int c){ c += c/3; for(int i = 0 ; i < 16 ; i++){ if((arr[d] >> i & 1) && i == c) return true; if((arr[d] >> i & 1) && i == c+1) return true; if((arr[d] >> i & 1) && i == c+4) return true; if((arr[d] >> i & 1) && i == c+5) return true; } return false; } void calc(int c,int *r){ if(c == 0){ r[0] = 0; }else{ r[0]++; } if(c == 2){ r[1] = 0; }else{ r[1]++; } if(c == 6){ r[2] = 0; }else{ r[2]++; } if(c == 8){ r[3] = 0; }else{ r[3]++; } } int getValue(int *r){ int res = 0; for(int i = 0 ; i < 4 ; i++){ res *= 7; res += r[i]; } return res; } int solve(){ int r[4] = {0}; calc(4,r); memset(visited,false,sizeof(visited)); visited[0][4][getValue(r)] = true; queue<State> Q; Q.push(State(0,4,r)); while(!Q.empty()){ State s = Q.front(); Q.pop(); int day = s.d,cloud = s.c; if(check(day,cloud)) continue; if(day == N-1) return 1; for(int i = 0 ; i < 5 ; i++){ int cc = cloud,nr[4]; for(int j = 0 ; j < 2 ; j++){ int nc = next(cc,i); if(nc == -1) break; memcpy(nr,s.r,sizeof(nr)); calc(nc,nr); if(*max_element(nr,nr+4) >= 7){ cc = nc; continue; } int value = getValue(nr); if(!visited[day+1][nc][value]){ visited[day+1][nc][value] = true; Q.push(State(day+1,nc,nr)); } cc = nc; if(i == 4) break; } } } return 0; } int main(){ while(cin >> N,N){ for(int i = 0 ; i < N ; i++){ int x,bit = 0; for(int j = 0 ; j < 16 ; j++){ cin >> x; if(x == 1){ bit |= (1<<j); } } arr[i] = bit; } cout << solve() << endl; } return 0; }
#include<iostream> #include<vector> #include<array> #include<set> using namespace std; struct S{ int y,x; array<array<int,4>,4> b; bool operator<(S s)const{ return (y!=s.y)?y<s.y:(x!=s.x)?x<s.x:b<s.b; } }; int main(){ for(int N;cin>>N,N;){ set<S> v[366]; v[0].insert({1,1,{}}); for(int i=0;i<N;i++){ int s[4][4]; for(int j=0;j<4;j++){ for(int k=0;k<4;k++){ cin>>s[j][k]; } } for(auto &e:v[i]){ for(int j=0;j<3;j++){ for(int k=0;k<3;k++){ if(i==0&&(j!=1||k!=1))continue; if(e.y!=j&&e.x!=k)continue; auto n=e.b; bool fail=false; for(int l=0;l<2;l++){ for(int m=0;m<2;m++){ fail|=s[j+l][k+m]; n[j+l][k+m]=-1; } } for(auto &f:n){ for(auto &g:f){ fail|=++g>=7; } } if(!fail){ v[i+1].insert({j,k,n}); } } } } } cout<<!v[N].empty()<<endl; } }
#include <cstring> #include <iostream> using namespace std; int n, a[375][22]; bool vis[375][3][3][7][7][7][7], dp[375][3][3][7][7][7][7]; int solve(int pos, int x, int y, int d1, int d2, int d3, int d4) { if (pos == n) return true; int b = x * 4 + y; if (a[pos][b] || a[pos][b + 1] || a[pos][b + 4] || a[pos][b + 5]) return false; if (vis[pos][x][y][d1][d2][d3][d4]) return dp[pos][x][y][d1][d2][d3][d4]; bool ret = false; for (int i = 0; i <= 2 && !ret; i++) { for (int j = 0; j <= 2 && !ret; j++) { if (i != x && j != y) continue; int nd1 = d1 + 1, nd2 = d2 + 1, nd3 = d3 + 1, nd4 = d4 + 1; if (i == 0 && j == 0) nd1 = 0; if (i == 0 && j == 2) nd2 = 0; if (i == 2 && j == 0) nd3 = 0; if (i == 2 && j == 2) nd4 = 0; if (nd1 < 7 && nd2 < 7 && nd3 < 7 && nd4 < 7 && solve(pos + 1, i, j, nd1, nd2, nd3, nd4)) ret = true; } } vis[pos][x][y][d1][d2][d3][d4] = true; dp[pos][x][y][d1][d2][d3][d4] = ret; return ret; } int main() { while (cin >> n, n) { for (int i = 0; i < n; i++) { for (int j = 0; j < 16; j++) { cin >> a[i][j]; } } memset(vis, 0, sizeof(vis)); memset(dp, 0, sizeof(dp)); bool ret = solve(0, 1, 1, 1, 1, 1, 1); cout << (ret ? 1 : 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ while (1){ int N; cin >> N; if (N == 0){ break; } vector<vector<vector<int>>> A(N, vector<vector<int>>(4, vector<int>(4))); for (int i = 0; i < N; i++){ for (int j = 0; j < 4; j++){ for (int k = 0; k < 4; k++){ cin >> A[i][j][k]; } } } vector<vector<vector<bool>>> ok(N, vector<vector<bool>>(3, vector<bool>(3, true))); for (int i = 0; i < N; i++){ for (int j = 0; j < 3; j++){ for (int k = 0; k < 3; k++){ for (int l = 0; l < 2; l++){ for (int m = 0; m < 2; m++){ if (A[i][j + l][k + m] == 1){ ok[i][j][k] = false; } } } } } } vector<vector<vector<vector<vector<vector<vector<bool>>>>>>> dp(N, vector<vector<vector<vector<vector<vector<bool>>>>>>(3, vector<vector<vector<vector<vector<bool>>>>>(3, vector<vector<vector<vector<bool>>>>(7, vector<vector<vector<bool>>>(7, vector<vector<bool>>(7, vector<bool>(7, false))))))); if (ok[0][1][1]){ dp[0][1][1][1][1][1][1] = true; } for (int i = 0; i < N - 1; i++){ for (int j = 0; j < 3; j++){ for (int k = 0; k < 3; k++){ for (int l = 0; l < 7; l++){ for (int m = 0; m < 7; m++){ for (int n = 0; n < 7; n++){ for (int o = 0; o < 7; o++){ if (dp[i][j][k][l][m][n][o]){ for (int p = 0; p < 3; p++){ for (int q = 0; q < 3; q++){ if (j == p || k == q){ if (ok[i + 1][p][q]){ int l2 = l + 1; int m2 = m + 1; int n2 = n + 1; int o2 = o + 1; if (p == 0 && q == 0){ l2 = 0; } if (p == 0 && q == 2){ m2 = 0; } if (p == 2 && q == 0){ n2 = 0; } if (p == 2 && q == 2){ o2 = 0; } if (l2 < 7 && m2 < 7 && n2 < 7 && o2 < 7){ dp[i + 1][p][q][l2][m2][n2][o2] = true; } } } } } } } } } } } } } bool ans = false; for (int i = 0; i < 3; i++){ for (int j = 0; j < 3; j++){ for (int k = 0; k < 7; k++){ for (int l = 0; l < 7; l++){ for (int m = 0; m < 7; m++){ for (int n = 0; n < 7; n++){ if (dp[N - 1][i][j][k][l][m][n]){ ans = true; } } } } } } } if (ans){ cout << 1 << endl; } else { cout << 0 << endl; } } }
#include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ #define H (4) #define W (4) #define HW (H * W) #define CH (H - 1) #define CW (W - 1) #define CHW (CH * CW) #define MAX_N (365) #define MAX_R (6) #define C_FALSE (1) #define C_TRUE (2) /* typedef */ typedef vector<int> vi; typedef vector<long long> vl; typedef vector<double> vd; typedef pair<int,int> pii; typedef pair<long,long> pll; typedef long long ll; struct pt_t { int x, y; }; /* global variables */ static pt_t dxys[] = { {0, 0}, {1, 0}, {2, 0}, {0, -1}, {0, -2}, {-1, 0}, {-2, 0}, {0, 1}, {0, 2} }; static int cache[MAX_N][CHW][MAX_R + 1][MAX_R + 1][MAX_R + 1][MAX_R + 1]; static int n; static bool fcs[MAX_N][HW]; static int ccvrs[CHW][4]; static vector<int> cnbrs[CHW]; /* subroutines */ void print_foo() { cout << "ccvrs: ["; for (int i = 0; i < CHW; i++) { if (i > 0) cout << ","; cout << "["; for (int j = 0; j < 4; j++) { if (j > 0) cout << ","; cout << ccvrs[i][j]; } cout << "]"; } cout << "]" << endl; cout << "cnbrs: ["; for (int i = 0; i < CHW; i++) { if (i > 0) cout << ","; cout << "["; for (int j = 0; j < cnbrs[i].size(); j++) { if (j > 0) cout << ","; cout << cnbrs[i][j]; } cout << "]"; } cout << "]" << endl; } void print_fcs() { cout << "fcs: ["; for (int i = 0; i < n; i++) { if (i > 0) cout << ","; cout << "["; for (int j = 0; j < HW; j++) { if (j > 0) cout << ","; cout << fcs[i][j]; } cout << "]"; } cout << "]" << endl; } int check_rec(int k, int cid, int r0, int r2, int r6, int r8) { //printf("k=%d,cid=%d,r0=%d,r2=%d,r6=%d,r8=%d\n", k, cid, r0, r2, r6, r8); if (k >= n) return C_TRUE; int& cc = cache[k][cid][r0][r2][r6][r8]; //cout << "cc = " << cc << endl; if (cc > 0) return cc; for (int i = 0; i < 4; i++) if (fcs[k][ccvrs[cid][i]]) { //printf("fcs[%d][%d] = %d\n", k, ccvrs[cid][i], fcs[k][ccvrs[cid][i]]); return (cc = C_FALSE); } int rr0 = r0 + 1; int rr2 = r2 + 1; int rr6 = r6 + 1; int rr8 = r8 + 1; switch (cid) { case 0: rr0 = 0; break; case 2: rr2 = 0; break; case 6: rr6 = 0; break; case 8: rr8 = 0; break; } if (rr0 > MAX_R || rr2 > MAX_R || rr6 > MAX_R || rr8 > MAX_R) return (cc = C_FALSE); for (int i = 0; i < cnbrs[cid].size(); i++) { int cid0 = cnbrs[cid][i]; if (check_rec(k + 1, cid0, rr0, rr2, rr6, rr8) == C_TRUE) return (cc = C_TRUE); } return (cc = C_FALSE); } /* main */ int main() { for (int y = 0; y < CH; y++) for (int x = 0; x < CW; x++) { int cid = y * CW + x; int cpos = y * W + x; ccvrs[cid][0] = cpos; ccvrs[cid][1] = cpos + 1; ccvrs[cid][2] = cpos + W; ccvrs[cid][3] = cpos + W + 1; cnbrs[cid].clear(); for (int d = 0; d < 9; d++) { int x0 = x + dxys[d].x; int y0 = y + dxys[d].y; if (x0 >= 0 && x0 < CW && y0 >= 0 && y0 < CH) { int cid0 = y0 * CW + x0; cnbrs[cid].push_back(cid0); } } sort(cnbrs[cid].begin(), cnbrs[cid].end()); } //print_foo(); exit(0); for (;;) { cin >> n; if (n == 0) break; memset(fcs, 0, sizeof(fcs)); int c; for (int i = 0; i < n; i++) for (int j = 0; j < HW; j++) { cin >> c; fcs[i][j] = (c == 1); } //print_fcs(); //cout << "sizeof(cache) = " << sizeof(cache) << endl; memset(cache, 0, sizeof(cache)); cout << (check_rec(0, 1 * CW + 1, 0, 0, 0, 0) - 1) << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; using Field=array<int,16>; using Event=vector<Field>; struct State{ int day; int pos; array<int,4> f; }; bool operator<(State lhs,State rhs){ return make_tuple(lhs.day,lhs.pos,lhs.f[0],lhs.f[1],lhs.f[2],lhs.f[3])<make_tuple(rhs.day,rhs.pos,rhs.f[0],rhs.f[1],rhs.f[2],rhs.f[3]); } set<State> s; int n; int dfs(State st,const Event &e); vector<vector<int>> nei= { {0,1,2,4,8}, {0,1,2,5,9}, {0,1,2,6,10}, {}, {0,4,8,5,6}, {1,4,9,5,6}, {2,4,10,5,6}, {}, {0,4,8,9,10}, {1,5,8,9,10}, {2,6,8,9,10}, {} }; int dfs(State st,const Event &e){ if(st.day==n) return true; if(s.count(st)) return false; s.insert(st); for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ if(e[st.day][st.pos+i*4+j]){ return false; } int x=st.pos+i*4+j; if(x==0) st.f[0]=-1; if(x==3) st.f[1]=-1; if(x==12) st.f[2]=-1; if(x==15) st.f[3]=-1; } } for(int i=0;i<4;i++){ if(++st.f[i]>=7) return false; } st.day++; int prep=st.pos; for(int i=0;i<nei[prep].size();i++){ st.pos=nei[prep][i]; if(dfs(st,e)) return true; } return false; } int solve(Event e){ State ini; ini.day=0; ini.pos=5; fill(ini.f.begin(),ini.f.end(),0); return dfs(ini,e); } int main(){ while(cin>>n,n){ s.clear(); Event event(n); for(int i=0;i<n;i++){ for(int j=0;j<16;j++) cin>>event[i][j]; } cout<<solve(event)<<endl; } return 0; }
#include<bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; bool dp[366][16][8][8][8][8]; int dx[]={-2,2,-1,1,0,0,0,0,0}; int dy[]={0,0,0,0,1,-1,0,2,-2}; int t[366][17],p[4],n; int main(){ while(cin>>n,n){ int ans=0; memset(dp,0,sizeof(dp)); r(i,n)r(j,16)cin>>t[i][j]; dp[0][5][1][1][1][1]=1; r(a,n-1)r(b,16)r(c,7)r(d,7)r(e,7)r(f,7) if(dp[a][b][c][d][e][f]){ int x1=b%4,y1=b/4; r(i,9){ int x=x1+dx[i],y=y1+dy[i],flag=0; if(x<0||y<0||2<x||2<y)continue; p[0]=y*4+x,p[1]=y*4+x+1; p[2]=(y+1)*4+x,p[3]=(y+1)*4+x+1; r(k,4)if(t[a+1][p[k]])flag++; if(!flag){ if(p[0]==0)dp[a+1][p[0]][0][d+1][e+1][f+1]=1; else if(p[0]==2)dp[a+1][p[0]][c+1][0][e+1][f+1]=1; else if(p[0]==8)dp[a+1][p[0]][c+1][d+1][0][f+1]=1; else if(p[0]==10)dp[a+1][p[0]][c+1][d+1][e+1][0]=1; else dp[a+1][p[0]][c+1][d+1][e+1][f+1]=1; } } } r(b,16)r(c,7)r(d,7)r(e,7)r(f,7) if(dp[n-1][b][c][d][e][f])ans++; if(t[0][5]||t[0][6]||t[0][9]||t[0][10])ans=0; cout<<(ans?1:0)<<endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define repi(i,a,b) for(int i = (a); i < (b); i++) #define rep(i,a) repi(i,0,a) int D, ds[512]; set<tuple<int,int,int>> done; bool dfs(int cl, int d, int cnt){ if(d<D and cl&ds[d]) return 0; auto v = make_tuple(cl,d,cnt); if(done.count(v)) return 0; done.insert(v); for(int i: {0, 3, 12, 15}){ int c = (cnt>>(i*3))&7; cnt ^= c<<(i*3); if(cl&(1LL<<i)) c = 0; else c++; if(c == 7) return 0; cnt ^= c<<(i*3); } if(d == D) return 1; rep(i,2) { if(dfs(cl,d+1,cnt)) return 1; if(!((4369<<i)&cl) and dfs(cl>>(i+1), d+1, cnt)) return 1; // left if(!((34952>>i)&cl) and dfs(cl<<(i+1), d+1, cnt)) return 1; // right if(!((15<<(i*4))&cl) and dfs(cl>>((i+1)*4), d+1, cnt)) return 1; // up if(!((61440>>(i*4))&cl) and dfs(cl<<((i+1)*4), d+1, cnt)) return 1; // down } return 0; } int solve(){ done.clear(); return dfs(1632, 0, 0); } bool input(){ cin >> D; if(!D) return 0; rep(i,D){ int b = 0; rep(j,16) { int a; cin >> a; b |= a << j; } ds[i] = b; } return 1; } signed main(){ while(input()){ cout << solve() << endl;} return 0; }
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<cstdlib> #include<vector> #include<stack> #include<algorithm> #include<queue> #include<string> #define OK 1 #define INF 2147483647 #define LINF 9223372036854775807LL #define DINF 100000000 #define LL long long using namespace std; typedef struct { int cnt[4][4]; }HNode; vector<HNode>H[366][4][4]; int n,map[365][4][4]; int biao[4][4][4][2]; const int di[] = { 0, 0, 0, -1, -2, 0, 0, 1, 2 }; const int dj[] = { 0, -1, -2, 0, 0, 1, 2, 0, 0 }; bool dfs(int cur, int ci, int cj, int cnt[4][4]); bool judge(int cur, int ci, int cj, int cnt[4][4]); int main() { int i,j,k,cnt[4][4]; while (scanf("%d", &n) > 0) { if (n == 0) break; for (k = 0; k < n; k++) { for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) H[k][i][j].clear(); } } for (k = 0; k < n; k++) { for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) scanf("%d", &map[k][i][j]); } } for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) cnt[i][j] = 1; } cnt[1][1] = cnt[1][2] = cnt[2][1] = cnt[2][2] = 0; judge(0, 1, 1, cnt); if (dfs(0, 1, 1, cnt) == true) printf("1\n"); else printf("0\n"); } return 0; } bool judge(int cur, int ci, int cj, int cnt[4][4]) { int k,j,i, len; bool flag = false; HNode e; for (k = 0, len = H[cur][ci][cj].size(); k < len; k++) { e = H[cur][ci][cj][k]; flag = true; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { if (e.cnt[i][j] != cnt[i][j]) flag = false; } } if (flag == true) break; } if (flag == false) { for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) e.cnt[i][j] = cnt[i][j]; } H[cur][ci][cj].push_back(e); return false; } else return true; } bool dfs(int cur,int ci,int cj,int cnt[4][4]) { int k,next_ci,next_cj,i,j,nextCnt[4][4]; if (map[cur][ci][cj] == 1 || map[cur][ci][cj + 1] == 1 || map[cur][ci + 1][cj] == 1 || map[cur][ci + 1][cj + 1] == 1) return false; if (cur == n - 1) return true; for (k = 0; k < 9; k++) { next_ci = ci + di[k]; next_cj = cj + dj[k]; if (next_ci < 0 || next_ci + 1 >= 4 || next_cj < 0 || next_cj + 1 >= 4) continue; memcpy(nextCnt, cnt, sizeof(nextCnt)); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) nextCnt[i][j]++; } nextCnt[next_ci][next_cj] = nextCnt[next_ci][next_cj + 1] = 0; nextCnt[next_ci + 1][next_cj] = nextCnt[next_ci + 1][next_cj + 1] = 0; bool flag = true; for (i = 0; i < 4; i++) //判断下雨天数 { for (j = 0; j < 4; j++) { if (nextCnt[i][j] == 7) flag = false; } } if (flag == false) continue; if (judge(cur + 1, next_ci, next_cj, nextCnt) == true) continue; if (dfs(cur + 1, next_ci, next_cj, nextCnt) == true) return true; } return false; }
#include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #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> using namespace std; #define REP(i,n) for(int i = 0; i < (int)n; i++) #define FOR(i,a,b) for(int i = a; i < (int)b; i++) #define pb push_back #define mp make_pair typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; typedef unsigned long long ull; const int INF = 1<<28; const ll MOD = 1000000007; int n; bool flg; vi a; set<ull> s; int d[9][9] = { {0, 1, 2, 3, 6, INF}, {-1, 0, 1, 3, 6, INF}, {-2, -1, 0, 3, 6, INF}, {-3, 0, 1, 2, 3, INF}, {-3, -1, 0, 1, 3, INF}, {-3, -2, -1, 0, 3, INF}, {-6, -3, 0, 1, 2, INF}, {-6, -3, -1, 0, 1, INF}, {-6, -3, -2, -1, 0, INF} }; bool chk(int cnt, int cl, string &now) { if(cl == 0 && (0b1100110000000000 & a[cnt]) == 0) { now[0] = now[1] = now[4] = now[5] = '7'; return true; } if(cl == 1 && (0b0110011000000000 & a[cnt]) == 0) { now[1] = now[2] = now[5] = now[6] = '7'; return true; } if(cl == 2 && (0b0011001100000000 & a[cnt]) == 0) { now[2] = now[3] = now[6] = now[7] = '7'; return true; } if(cl == 3 && (0b0000110011000000 & a[cnt]) == 0) { now[4] = now[5] = now[8] = now[9] = '7'; return true; } if(cl == 4 && (0b0000011001100000 & a[cnt]) == 0) { now[5] = now[6] = now[9] = now[10] = '7'; return true; } if(cl == 5 && (0b0000001100110000 & a[cnt]) == 0) { now[6] = now[7] = now[10] = now[11] = '7'; return true; } if(cl == 6 && (0b0000000011001100 & a[cnt]) == 0) { now[8] = now[9] = now[12] = now[13] = '7'; return true; } if(cl == 7 && (0b0000000001100110 & a[cnt]) == 0) { now[9] = now[10] = now[13] = now[14] = '7'; return true; } if(cl == 8 && (0b0000000000110011 & a[cnt]) == 0) { now[10] = now[11] = now[14] = now[15] = '7'; return true; } return false; } void dfs(int cnt, int cl, string now) { if(flg) return; REP(i, 16) { if(now[i] == '0') return; now[i]--; } if(cnt == n) flg = true; REP(i, 9) { if(d[cl][i] == INF) break; string now2 = now; if(chk(cnt, cl + d[cl][i], now2)) { ull u = stoull(now2) * 10000 + cnt + 10 + cl+d[cl][i]; if(!s.count(u)) { s.insert(u); dfs(cnt+1, cl + d[cl][i], now2); } } } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); while(cin >> n, n) { flg = false; a.clear(); s.clear(); REP(i, n) { a.pb(0); REP(j, 16) { int in; cin >> in; if(j) a[i] <<= 1; a[i] |= in; } } if((a[0] & 0b0000011001100000)) { cout << 0 << endl; continue; } dfs(1, 4, "6666677667766666"); cout << flg << endl; } return 0; }
#include<iostream> #include<algorithm> #include<set> using namespace std; struct S{ int g[4][4]; int y,x; bool operator<(S s)const{ return lexicographical_compare(g[0],g[4],s.g[0],s.g[4]); } }; int main(){ for(int N;cin>>N,N;){ set<S> d[366]; S s{{},1,1}; d[0].insert(s); for(int i=0;i<N;i++){ int s[16]; for(int j=0;j<16;j++){ cin>>s[j]; } for(auto e:d[i]){ for(int k=0;k<3;k++){ for(int l=0;l<3;l++){ if(k!=e.y&&l!=e.x||i==0&&(k!=1||l!=1))continue; auto cs=e; cs.y=k; cs.x=l; for(int m=0;m<4;m++){ for(int n=0;n<4;n++){ cs.g[m][n]++; } } for(int m=0;m<2;m++){ for(int n=0;n<2;n++){ int y=k+m; int x=l+n; cs.g[y][x]=0; if(s[y*4+x])goto next; } } if(*max_element(cs.g[0],cs.g[4])>=7)goto next; d[i+1].insert(cs); next: ; } } } } cout<<!d[N].empty()<<endl; } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ map<int,int> mp; vector<vector<int>> nextS(9); void init(){ int masu[4][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, }; int idx = 0; for(int i = 0; i < 3;i++){ for(int j = 0; j < 3;j++){ int S = 0; S = (1<<masu[i][j]) + (1<<masu[i][j+1]) + (1<<masu[i+1][j]) + (1<<masu[i+1][j+1]); mp[S] = idx++; //cout << S << " "; } //cout << endl; } /* 51 102 204 816 1632 3264 13056 26112 52224 */ nextS[0] = vector<int>{51,102,204,816,13056}; nextS[1] = vector<int>{51,102,204,1632,26112}; nextS[2] = vector<int>{51,102,204,3264,52224}; nextS[3] = vector<int>{51,816,1632,3264,13056}; nextS[4] = vector<int>{102,816,1632,3264,26112}; nextS[5] = vector<int>{204,816,1632,3264,52224}; nextS[6] = vector<int>{51,816,13056,26112,52224}; nextS[7] = vector<int>{102,1632,13056,26112,52224}; nextS[8] = vector<int>{204,3264,13056,26112,52224}; } set<tuple<int,int,ll>> memo; bool dfs(int n,int S,ll clowdcnt,const vector<int>& state){ if((n < state.size()) && (S & state[n])) return false; if(memo.find(tuple<int,int,ll>(n,S,clowdcnt))!=memo.end()) return false; memo.insert(tuple<int,int,ll>(n,S,clowdcnt)); for(int i = 0; i < 16; i++){ ll nonrain = (clowdcnt>>(i*3))&7; clowdcnt ^= nonrain<<(i*3); nonrain++; if((S>>i)&1) nonrain = 0; if(nonrain == 7) return false; clowdcnt |= (nonrain<<(i*3)); } if(n == state.size()) return true; int idx = mp[S]; for(auto next:nextS[idx]){ if(dfs(n+1,next,clowdcnt,state))return true; } return false; } bool solve(int N){ memo.clear(); vector<int> state(N); for(int i = 0; i < N;i++){ int B = 0; for(int j = 0; j < 16;j++){ int c; cin >> c; B += (c<<j); } state[i] = B; // cout << bitset<16>(state[i]) << endl; } int beginS = (1<<5) + (1<<6) + (1<<9) + (1<<10); return dfs(0,beginS,0,state); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); int N; while(cin >> N,N){ cout << solve(N) << endl; } return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) #define INF 100000000 #define EPS 1e-10 #define MOD 1000000007 using namespace std; typedef pair<int,int> P; int n; int f[4][4]; bool dp[400][3][3][7][7][7][7]; void solve(){ rep(i,400) rep(j1,3) rep(j2,3) rep(k1,7) rep(k2,7) rep(k3,7) rep(k4,7) dp[i][j1][j2][k1][k2][k3][k4] = false; bool ok = true; rep(i,n){ rep(j,4) rep(k,4) cin >> f[j][k]; if(!ok) continue; if(i == 0){ if(f[1][1]+f[1][2]+f[2][1]+f[2][2] > 0){ ok = false; continue; } dp[0][1][1][1][1][1][1] = true; continue; } rep(j,3) rep(k,3){ if(f[j][k]+f[j+1][k]+f[j][k+1]+f[j+1][k+1] > 0) continue; rep(j1,3) rep(k1,3){ if(abs(j1-j)>0 && abs(k1-k)>0) continue; rep(l1,7) rep(l2,7) rep(l3,7) rep(l4,7){ if(j==0&&k==0){ if(l2 == 0 || l3 == 0 || l4 == 0) continue; rep(x,7) dp[i][j][k][0][l2][l3][l4] |= dp[i-1][j1][k1][x][l2-1][l3-1][l4-1]; } else if(j==0&&k==2){ if(l1 == 0 || l3 == 0 || l4 == 0) continue; rep(x,7) dp[i][j][k][l1][0][l3][l4] |= dp[i-1][j1][k1][l1-1][x][l3-1][l4-1]; } else if(j==2&&k==0){ if(l1 == 0 || l2 == 0 || l4 == 0) continue; rep(x,7) dp[i][j][k][l1][l2][0][l4] |= dp[i-1][j1][k1][l1-1][l2-1][x][l4-1]; } else if(j==2&&k==2){ if(l1 == 0 || l2 == 0 || l3 == 0) continue; rep(x,7) dp[i][j][k][l1][l2][l3][0] |= dp[i-1][j1][k1][l1-1][l2-1][l3-1][x]; }else { if(l1 == 0 || l2 == 0 || l3 == 0 || l4 == 0) continue; dp[i][j][k][l1][l2][l3][l4] |= dp[i-1][j1][k1][l1-1][l2-1][l3-1][l4-1]; } } } } bool yes = false; rep(j,3) rep(k,3) rep(l1,7) rep(l2,7) rep(l3,7) rep(l4,7) if(dp[i][j][k][l1][l2][l3][l4]) yes = true; if(!yes){ ok = false; continue; } } if(!ok) puts("0"); else puts("1"); } int main(){ while(cin >> n){ if(n == 0) break; solve(); } }
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <map> #include <queue> #include <deque> #include <stack> #include <algorithm> #include <cstring> #include <functional> #include <cmath> 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 fs first #define sc second #define pb push_back #define show(x) cout << #x << " " << x << endl struct cl{int v[4],x,y;}; int dx[9]={0,0,0,0,0,1,-1,2,-2}; int dy[9]={0,1,-1,2,-2,0,0,0,0}; int valid(int x,int y){ return 0<=x&&x<3&&0<=y&&y<3; } bool operator<(const cl& x,const cl& y){ rep(i,4) if(x.v[i]!=y.v[i]) return x.v[i]<y.v[i]; return x.x*4+x.y<y.x*4+y.y; } bool operator==(const cl& x,const cl& y){ rep(i,4) if(x.v[i]!=y.v[i]) return false; return x.x*4+x.y==y.x*4+y.y; } int main(){ while(true){ int N; cin>>N; if(N==0) break; cl be; rep(i,4) be.v[i]=0; be.x=1,be.y=1; vector<cl> vc,nvc; vc.pb(be); rep(i,N){ int f[4][4]; rep(j,4) rep(k,4) cin>>f[j][k]; for(auto cloud:vc){ int v[4]; rep(j,4) v[j]=cloud.v[j]+1; int x=cloud.x,y=cloud.y; if(x==0&&y==0) v[0]=0; if(x==0&&y==2) v[1]=0; if(x==2&&y==0) v[2]=0; if(x==2&&y==2) v[3]=0; bool can=true; rep(ii,2) rep(jj,2) if(f[x+ii][y+jj]) can=false; rep(j,4) if(v[j]==7) can=false; if(!can) continue; rep(j,9){ int nx=x+dx[j],ny=y+dy[j]; if(!valid(nx,ny)) continue; cl hoge; rep(k,4) hoge.v[k]=v[k]; hoge.x=nx,hoge.y=ny; nvc.pb(hoge); } } sort(all(nvc)); nvc.erase(unique(all(nvc)),nvc.end()); vc=nvc; nvc.clear(); // show(vc.size()); } cout<<(vc.size()?1:0)<<endl; } }
#include<iostream> #include<sstream> #include<vector> #include<set> #include<map> #include<queue> #include<algorithm> #include<numeric> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #define rep(i,n) for(int i=0;i<n;i++) #define all(c) (c).begin(),(c).end() #define mp make_pair #define pb push_back #define rp(i,c) rep(i,(c).size()) #define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++) #define dbg(x) cerr<<#x<<" = "<<(x)<<endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; const int inf=1<<28; const double INF=1e12,EPS=1e-9; int n; bool market[365][16]; bool V[366][2401][9]; bool check(int day,int y,int x) { rep(i,2)rep(j,2)if(market[day][(y+i)*4+x+j])return 0; return 1; } bool visit(int day,int y,int x,int r0,int r1,int r2,int r3) { bool &v=V[day][343*r0+49*r1+7*r2+r3][y*3+x]; if(v)return 1; v=1; return 0; } bool dfs(int day,int cy,int cx,int r0,int r1,int r2,int r3) { if(day>=n)return 1; for(int ny=cy-2;ny<=cy+2;ny++)for(int nx=cx-2;nx<=cx+2;nx++) if(ny==cy||nx==cx) if(0<=ny&&ny<3&&0<=nx&&nx<3) { if(!check(day,ny,nx))continue; int nr0=r0,nr1=r1,nr2=r2,nr3=r3; if(ny==0&&nx==0)nr0=0; else nr0++; if(ny==0&&nx==2)nr1=0; else nr1++; if(ny==2&&nx==0)nr2=0; else nr2++; if(ny==2&&nx==2)nr3=0; else nr3++; if(nr0>=7||nr1>=7||nr2>=7||nr3>=7)continue; if(visit(day+1,ny,nx,nr0,nr1,nr2,nr3))continue; if(dfs(day+1,ny,nx,nr0,nr1,nr2,nr3))return 1; } return 0; } int main() { while(scanf("%d",&n),n) { rep(i,n)rep(j,16)scanf("%d",market[i]+j); rep(i,n+1)rep(j,2401)fill_n(V[i][j],9,0); if(!check(0,1,1)) { cout<<0<<endl; continue; } cout<<dfs(1,1,1,1,1,1,1)<<endl; } return 0; }
#include <iostream> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <iomanip> #include <cmath> #include <cassert> #include <set> #include <tuple> #include <unordered_set> using namespace std; #define int long long set<tuple<int, int, int> > done; int D; int ds[365]; int dfs(int cl, int d, int cnt) { if(d < D && (cl & ds[d])) return 0; auto v = make_tuple(cl, d, cnt); if(done.count(v)) return 0; done.insert(v); for(int i = 0; i < 16; i++) { int c = (cnt >> (i * 3)) & 7; cnt ^= c << (i * 3); if(cl & (1LL << i)) c = 0; else c++; if(c == 7) return 0; cnt ^= c << (i * 3); } if(d == D) return 1; for(int i = 0; i < 2; i++) { if(dfs(cl, d + 1, cnt)) return 1; if(!((4369 << i) & cl) && dfs(cl >> (i + 1), d + 1, cnt)) return 1; if(!((34952 >> i) & cl) && dfs(cl << (i + 1), d + 1, cnt)) return 1; if(!((15 << (i * 4)) & cl) && dfs(cl >> ((i + 1) * 4), d + 1, cnt)) return 1; if(!((61440 >> (i * 4)) & cl) && dfs(cl << ((i + 1) * 4), d + 1, cnt)) return 1; } return 0; } void solve() { for(int i = 0; i < D; i++) { ds[i] = 0; for(int j = 0; j < 16; j++) { int a; cin >> a; ds[i] |= a << j; } } done.clear(); cout << dfs(1632, 0, 0) << endl; } signed main() { cin.tie(0); ios::sync_with_stdio(false); while(cin >> D, D) { solve(); } }
#include <bits/stdc++.h> #define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i)) #define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i)) #define rep(i, n) For(i, 0, n) #define rrep(i, n) rFor(i, n, 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;} template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;} template<class T> T div_floor(T a, T b){ if(b < 0) a *= -1, b *= -1; return a>=0 ? a/b : (a+1)/b-1; } template<class T> T div_ceil(T a, T b){ if(b < 0) a *= -1, b *= -1; return a>0 ? (a-1)/b+1 : a/b; } constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 100010; set<pair<pii, array<int, 4>>> S; int n, a[370][4][4]; int dx[9] = {0, 0, 1, 0, -1, 0, 2, 0, -2}; int dy[9] = {0, 1, 0, -1, 0, 2, 0, -2, 0}; bool dfs(int cur, int pos, array<int, 4> rain){ if(cur == n){ //printf("%d (%d, %d)\n", cur, pos / 4, pos % 4); return true; } if(S.find({{cur, pos}, rain}) != S.end()) return false; int x = pos / 4, y = pos % 4; rep(i, 2)rep(j, 2){ int nx = x + i, ny = y + j; if(a[cur][nx][ny]){ S.insert({{cur, pos}, rain}); return false; } } array<int, 4> nxt_rain; rep(i, 4) nxt_rain[i] = rain[i]; if(pos == 0) nxt_rain[0] = -1; if(pos == 2) nxt_rain[1] = -1; if(pos == 8) nxt_rain[2] = -1; if(pos == 10) nxt_rain[3] = -1; rep(i, 4){ ++nxt_rain[i]; if(nxt_rain[i] == 7){ S.insert({{cur, pos}, rain}); return false; } } rep(i, 9){ int nx = x + dx[i], ny = y + dy[i]; if(0 <= nx && nx < 3 && 0 <= ny && ny < 3){ if(dfs(cur + 1, nx * 4 + ny, nxt_rain)){ //printf("%d (%d, %d)\n", cur, pos / 4, pos % 4); return true; } } } S.insert({{cur, pos}, rain}); return false; } int main(){ while(scanf("%d", &n) && n){ S.clear(); rep(i, n)rep(x, 4)rep(y, 4) scanf("%d", &a[i][x][y]); printf("%d\n", dfs(0, 5, {0, 0, 0, 0})); } }
#include <iostream> #include <vector> #include <set> #include <utility> const int d[] = {0,1,2,4,8,-1,-2,-4,-8}, d_[] = {0,1,4,5}; using namespace std; int main(){ int N; while(cin >> N, N){ vector< vector<int> > V(N, vector<int>(16)); for(int i = 0; i < N; ++i) for(int j = 0; j < 16; ++j) cin >> V[i][j]; vector<int> s(16,1); bool f = false; for(int i = 0; i < 4; ++i){ if(V[0][5+d_[i]]){ f = true; break; } s[5+d_[i]] = 0; } if(f){ cout << 0 << endl; continue; } set< pair<int, vector<int> > > S; S.insert(make_pair(5,s)); for(int i = 1; i < N; ++i){ set< pair<int, vector<int> > > S_; for(auto itr = S.begin(); itr != S.end(); ++itr){ int t = (*itr).first; vector<int> v = (*itr).second; for(int j = 0; j < 16; ++j) ++v[j]; for(int j = 0; j < 9; ++j){ int t_ = t + d[j]; if(t_ < 0 || t_%4 == 3 || t_/4 >= 3 || (t%4 != t_%4 && t/4 != t_/4)) continue; vector<int> v_ = v; bool f = false; for(int k = 0; k < 4; ++k){ if(V[i][t_+d_[k]]){ f = true; break; } v_[t_+d_[k]] = 0; } for(int k = 0; k < 16; ++k) if(v_[k] == 7) f = true; if(f) continue; S_.insert(make_pair(t_,v_)); } } S = S_; } if(!S.empty()) cout << 1 << endl; else cout << 0 << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; int n; vector<int> v; typedef pair<int,vector<int> > P; map<P,bool> mv; int ax[]={0,1,2,-1,-2,0,0,0,0}; int ay[]={0,0,0,0,0,1,2,-1,-2}; int in(int y,int x){ return 0<=y&&y<3&&0<=x&&x<3; } int in2(int y,int x){ return 0<=y&&y<4&&0<=x&&x<4; } bool solve(int d,vector<int> m){ if(d==n-1) return true; if(mv.count(P(d,m))) return mv[P(d,m)]; int y=m[0]/3,x=m[0]%3; for(int k=0;k<9;k++){ int ny=y+ay[k],nx=x+ax[k]; if(!in(ny,nx)) continue; bool f=0; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ int ty=ny+i,tx=nx+j; if(!in2(ty,tx)) continue; if((v[d+1]>>(ty*4+tx))&1) f=1; } } if(f) continue; if(d>4){ int a[4][4]={}; for(int l=0;l<6;l++){ int dy=m[l]/3,dx=m[l]%3; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ int ty=dy+i,tx=dx+j; if(!in2(ty,tx)) continue; a[ty][tx]++; } } } for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ int ty=ny+i,tx=nx+j; if(!in2(ty,tx)) continue; a[ty][tx]++; } } for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(!a[i][j]) f=1; } vector<int> u(6); for(int i=1;i<6;i++) u[i]=m[i-1]; u[0]=ny*3+nx; if(f) continue; if(solve(d+1,u)) return mv[P(d,m)]=true; } return mv[P(d,m)]=false; } signed main(){ while(cin>>n,n){ v.resize(n); for(int i=0;i<n;i++){ v[i]=0; for(int j=0,k;j<16;j++){ cin>>k; v[i]+=(1<<j)*k; } } vector<int> m(6,-1); m[0]=4; mv.clear(); bool f=0; for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ int ty=1+i,tx=1+j; if((v[0]>>(ty*4+tx))&1) f=1; } } //cout<<"f:"<<f<<endl; if(f) cout<<0<<endl; else cout<<solve(0,m)<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=(x);i<(y);++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define dump(x) #endif typedef long long int ll; typedef pair<int,int> pii; //template<typename T> using vec=std::vector<T>; const int inf=1<<30; const long long int infll=1LL<<58; const double eps=1e-9; const int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve(){ while(true){ int n; cin >> n; if(n==0) break; vector<vector<int>> schedule(n,vector<int>(16)); rep(i,0,n) for(int& in:schedule[i]) cin >> in; set<tuple<int,int,int>> done; const int mask=(1<<3)-1; function<bool(int,int,int)> dfs=[&](int day,int pos,int cnt){ tuple<int,int,int> state(day,pos,cnt); if(done.find(state)!=done.end()) return false; done.insert(state); if(schedule[day][pos] or schedule[day][pos+1] or schedule[day][pos+4] or schedule[day][pos+5]) return false; int cnt1=cnt&mask,cnt2=(cnt>>3)&mask,cnt3=(cnt>>6)&mask,cnt4=(cnt>>9)&mask; ++cnt1; ++cnt2; ++cnt3; ++cnt4; if(pos==0) cnt1=0; if(pos==2) cnt2=0; if(pos==8) cnt3=0; if(pos==10) cnt4=0; if(cnt1>6 or cnt2>6 or cnt3>6 or cnt4>6) return false; if(day==n-1) return true; int next_cnt=cnt1|(cnt2<<3)|(cnt3<<6)|(cnt4<<9); int x=pos%4,y=pos/4; bool res=false; rep(d1,-2,3) rep(d2,-2,3){ if(d1!=0 and d2!=0) continue; int nx=x+d1,ny=y+d2; if(nx<0 or 2<nx or ny<0 or 2<ny) continue; int next_pos=nx+ny*4; res|=dfs(day+1,next_pos,next_cnt); } return res; }; if(dfs(0,5,0)) cout << 1 << endl; else cout << 0 << endl; } } int main(){ std::ios::sync_with_stdio(false); std::cin.tie(0); cout << fixed << setprecision(8); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 400; const int R = 4; const int W = 7; const int di[] = {0,1,0,-1}; const int dj[] = {1,0,-1,0}; int N; int F[MAXN][R][R]; bool dp[MAXN][R][R][W][W][W][W]; int main() { while(cin >> N && N) { for(int k = 0; k < N; ++k) for(int i = 0; i < R; ++i) for(int j = 0; j < R; ++j) cin >> F[k][i][j]; memset(dp, 0, sizeof(dp)); dp[0][1][1][0][0][0][0] = 1; bool ok = false; for(int k = 0; k < N; ++k) for(int i = 0; i < R; ++i) for(int j = 0; j < R; ++j) for(int a = 0; a < W; ++a) for(int b = 0; b < W; ++b) for(int c = 0; c < W; ++c) for(int d = 0; d < W; ++d) { if(!dp[k][i][j][a][b][c][d]) continue; for(int s = 0; s < 4; ++s) for(int l = 0; l <= 2; ++l) { if(k == 0 && l) continue; int ni = i + di[s] * l; int nj = j + dj[s] * l; if(ni < 0 || ni + 1 >= R) continue; if(nj < 0 || nj + 1 >= R) continue; if(F[k][ni][nj]) continue; if(F[k][ni][nj+1]) continue; if(F[k][ni+1][nj]) continue; if(F[k][ni+1][nj+1]) continue; int na = (ni == 0 && nj == 0 ? 0 : a + 1); int nb = (ni == 0 && nj+1 == R-1 ? 0 : b + 1); int nc = (ni+1 == R-1 && nj == 0 ? 0 : c + 1); int nd = (ni+1 == R-1 && nj+1 == R-1 ? 0 : d + 1); if(max(max(na,nb),max(nc,nd)) >= W) continue; dp[k+1][ni][nj][na][nb][nc][nd] = true; if(k+1 == N) ok = true; } } cout << ok << endl; } return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <queue> #include <algorithm> #define llint long long #define inf 1e18 using namespace std; llint n; bool dp[405][3][3][7][7][7][7]; bool sch[405][4][4]; llint dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1}; bool check(llint d, llint x, llint y) { if(sch[d][x][y] || sch[d][x+1][y] || sch[d][x][y+1] || sch[d][x+1][y+1]) return false; return true; } int main(void) { while(1){ cin >> n; if(n == 0) break; for(int i = 1; i <= n; i++){ for(int j = 0; j < 16; j++){ cin >> sch[i][j%4][j/4]; } } for(int d = 0; d <= n; d++){ for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ for(int i = 0; i < 7; i++){ for(int j = 0; j < 7;j++){ for(int k = 0; k < 7; k++){ for(int l = 0; l < 7; l++){ dp[d][x][y][i][j][k][l] = false; } } } } } } } if(!check(1, 1, 1)){ cout << 0 << endl; continue; } dp[1][1][1][1][1][1][1] = true; for(int d = 1; d < n; d++){ for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ for(int i = 0; i < 7; i++){ for(int j = 0; j < 7;j++){ for(int k = 0; k < 7; k++){ for(int l = 0; l < 7; l++){ if(!dp[d][x][y][i][j][k][l]) continue; for(int s = 0; s < 4; s++){ for(int t = 0; t <= 2; t++){ llint nx = x + t*dx[s], ny = y + t*dy[s]; if(nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; if(!check(d+1, nx, ny)) continue; llint ni = i+1, nj = j+1, nk = k+1, nl = l+1; if(nx == 0 && ny == 0) ni = 0; if(nx == 0 && ny == 2) nj = 0; if(nx == 2 && ny == 2) nk = 0; if(nx == 2 && ny == 0) nl = 0; if(ni >= 7 || nj >= 7 || nk >= 7 || nl >= 7) continue; dp[d+1][nx][ny][ni][nj][nk][nl] = true; } } } } } } } } } bool ans = false; for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ for(int i = 0; i < 7; i++){ for(int j = 0; j < 7;j++){ for(int k = 0; k < 7; k++){ for(int l = 0; l < 7; l++){ ans |= dp[n][x][y][i][j][k][l]; } } } } } } if(ans) cout << 1 << endl; else cout << 0 << endl; } return 0; }
#include<bits/stdc++.h> #define N 366 #define L 16 #define M 9 #define D 7 using namespace std; bool dp[N][M][D][D][D][D]; int d[N][L]; int dy[5]={0,-1,0,1,0}; int dx[5]={0,0,1,0,-1}; int n; int main(){ while(1){ cin>>n; if(!n)break; memset(d,0,sizeof(d)); for(int i=0;i<n;i++) for(int j=0;j<L;j++) cin>>d[i][j]; memset(dp,0,sizeof(dp)); dp[0][4][1][1][1][1]=true; bool ans=false; for(int i=0;i<n-1;i++) for(int j=0;j<M;j++) for(int k1=0;k1<D;k1++) for(int k2=0;k2<D;k2++) for(int k3=0;k3<D;k3++) for(int k4=0;k4<D;k4++){ if(!dp[i][j][k1][k2][k3][k4])continue; int y=j/3,x=j%3; for(int l=0;l<5;l++){ for(int l2=0;l2<5;l2++){ if(!(l2==0||l==l2))continue; int ny=y+dy[l]+dy[l2],nx=x+dx[l]+dx[l2]; if(ny<0||nx<0||3<=ny||3<=nx)continue; int nj=ny*3+nx,flag=1; if(nj<3){ if(d[i+1][nj]||d[i+1][nj+1]||d[i+1][nj+4]||d[i+1][nj+5])flag=0; }else if(nj<6){ if(d[i+1][nj+1]||d[i+1][nj+2]||d[i+1][nj+5]||d[i+1][nj+6])flag=0; } else if(d[i+1][nj+2]||d[i+1][nj+3]||d[i+1][nj+6]||d[i+1][nj+7])flag=0; int nk1=k1,nk2=k2,nk3=k3,nk4=k4; if(nj==0)nk1=0; else if(nk1==6)flag=0; else nk1++; if(nj==2)nk2=0; else if(nk2==6)flag=0; else nk2++; if(nj==6)nk3=0; else if(nk3==6)flag=0; else nk3++; if(nj==8)nk4=0; else if(nk4==6)flag=0; else nk4++; if(flag)dp[i+1][nj][nk1][nk2][nk3][nk4]=true; } } } for(int i=0;i<M;i++) for(int k1=0;k1<D;k1++) for(int k2=0;k2<D;k2++) for(int k3=0;k3<D;k3++) for(int k4=0;k4<D;k4++) if(dp[n-1][i][k1][k2][k3][k4])ans=true; if(d[0][5]||d[0][6] ||d[0][9]||d[0][10])ans=false; cout<<ans<<endl; } return 0; }
#include<iostream> #include<algorithm> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) bool vis[366][4][4][8][8][8][8]={0}; bool isevent[366][4][4]; int dx[]={-2,-1, 0, 0,1,2,0,0,0}; int dy[]={ 0, 0,-2,-1,0,0,1,2,0}; bool canstay(int day,int tary,int tarx){ rep(i,2){ rep(j,2){ if (isevent[day][tary+i][tarx+j])return false; } } return true; } bool is_rain_full(int cur[4][4]){ rep(i,4){ rep(j,4){ if (cur[i][j] == 7)return false; } } return true; } void rain(int ney,int nex,int cur[4][4]){ rep(i,4){ rep(j,4){ cur[i][j]++; } } rep(i,2){ rep(j,2){ cur[ney+i][nex+j]=0; } } } bool dfs(int n,int now,int y,int x, int lu,int ru,int ld,int rd, int cur[4][4]){ if (!is_rain_full(cur))return false; if (n == now){ return true; } if (!canstay(now,y,x) )return false; rain(y,x,cur); if (vis[now][y][x][lu][ru][ld][rd])return false; vis[now][y][x][lu][ru][ld][rd]=true; int tmp[4][4]; rep(k,9){ int nex=x+dx[k],ney=y+dy[k]; if (nex < 0|| ney < 0|| nex > 2|| ney > 2)continue; rep(i,4)rep(j,4)tmp[i][j]=cur[i][j]; if (dfs(n,now+1,ney,nex, cur[0][0],cur[0][3], cur[3][0],cur[3][3], cur))return true; rep(i,4)rep(j,4)cur[i][j]=tmp[i][j]; } return false; } int main(){ int n; while(cin>>n && n){ rep(k,n){ rep(i,4) rep(j,4)cin>>isevent[k][i][j]; } rep(i,4)rep(j,4)isevent[n][i][j]=0; rep(i,n) rep(j,4) rep(k,4) rep(l,8) rep(m,8) rep(o,8) rep(p,8) vis[i][j][k][l][m][o][p]=false; int cur[4][4]={0}; cout << dfs(n,0,1,1,0,0,0,0,cur)<<endl; } return false; }
#include <cstdio> const int MAXN = 366; const int move[] = {0, 1, 2, -1, -2, 4, 8, -4, -8}; int Day; int rain[MAXN+10][20]; int vis[MAXN][12][7][7][7][7], T; int dfs(int D, int pos, int n1, int n4, int n13, int n16){ if(pos==1) n1 = 0; //1 2 3 4 if(pos==3) n4 = 0; //5 6 7 8 if(pos==9) n13= 0; //9 10 11 12 if(pos==11)n16= 0; //13 14 15 16 if(n1>=7 || n4>=7 || n13>=7 || n16>=7) return 0; if(D==Day+1) return 1; if(rain[D][pos]) return 0; if(rain[D][pos+1]) return 0; if(rain[D][pos+4]) return 0; if(rain[D][pos+5]) return 0; if(vis[D][pos][n1][n4][n13][n16]==T) return 0; vis[D][pos][n1][n4][n13][n16] = T; for(int i=0;i<9;i++){ int npos = pos + move[i]; if(npos%4 != (pos%4)+(move[i]%4) ) continue; if(npos<1 || 11<npos || npos==4 || npos==8) continue; if(dfs(D+1, npos, n1+1, n4+1, n13+1, n16+1)) return 1; } return 0; } int main(){ while(scanf("%d", &Day)){ if(Day==0) break; for(int i=1;i<=Day;i++){ for(int j=1;j<=16;j++){ scanf("%d", &rain[i][j]); } } T++; printf("%d\n", dfs(1, 6, 1, 1, 1, 1)); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<ll, ll>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() template<typename T> void chmin(T &a, const T &b) { a = min(a, b); } template<typename T> void chmax(T &a, const T &b) { a = max(a, b); } struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio; #ifdef DEBUG_ #include "../program_contest_library/memo/dump.hpp" #else #define dump(...) #endif const ll INF = 1LL<<60; bool dp[366][9][7][7][7][7]; int main(void) { while(1) { ll n; cin >> n; if(!n) break; vector<vector<ll>> v(n, vector<ll>(16)); REP(i, n) REP(j, 16) cin >> v[i][j]; // REP(i, n) { // cerr << "i:" << i << endl; // REP(j, 4) { // REP(k, 4) cerr << v[i][j*4+k] << " "; // cerr << endl; // } // } if(v[0][5] || v[0][6] || v[0][9] || v[0][10]) { cout << 0 << endl; continue; } REP(i, n) REP(j, 9) REP(d0, 7) REP(d1, 7) REP(d2, 7) REP(d3, 7) { dp[i][j][d0][d1][d2][d3] = false; } dp[0][4][1][1][1][1] = true; FOR(i, 1, n) REP(j0, 9) { const ll j = j0 + j0/3; const ll x = j%4, y = j/4; REP(d0, 7) REP(d1, 7) REP(d2, 7) REP(d3, 7) { if(!dp[i-1][j0][d0][d1][d2][d3]) continue; REP(nj0, 9) { const ll nj = nj0 + nj0/3; const ll nx = nj%4, ny = nj/4; if(abs(x-nx) + abs(y-ny) > 2) continue; if(x!=nx && y!=ny) continue; if(v[i][nj] || v[i][nj+1] || v[i][nj+4] || v[i][nj+5]) continue; if(nj == 0 && d1<6 && d2<6 && d3<6) { dp[i][nj0][0][d1+1][d2+1][d3+1] = true; } else if(nj == 2 && d0<6 && d2<6 && d3<6) { dp[i][nj0][d0+1][0][d2+1][d3+1] = true; } else if(nj == 8 && d0<6 && d1<6 && d3<6) { dp[i][nj0][d0+1][d1+1][0][d3+1] = true; } else if(nj == 10 && d0<6 && d1<6 && d2<6) { dp[i][nj0][d0+1][d1+1][d2+1][0] = true; } else if(d0<6 && d1<6 && d2<6 && d3<6) { dp[i][nj0][d0+1][d1+1][d2+1][d3+1] = true; } } } } ll ret = 0; REP(i, 9) REP(d0, 7) REP(d1, 7) REP(d2, 7) REP(d3, 7) { if(dp[n-1][i][d0][d1][d2][d3]) { ret = 1; } } cout << ret << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstring> #include <cmath> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; struct edge { int u, v; ll w; }; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int enc(int x, int y) { return x + y * 3; } int _enc(int a, int b, int c, int d) { return (a << 9) + (b << 6) + (c << 3) + d; } int main() { for (;;) { int N; cin >> N; if (N == 0) break; vector< vector<bool> > dp(9, vector<bool>(4096)); dp[4][_enc(1, 1, 1, 1)] = true; while (N--) { vector< vector<bool> > v(3, vector<bool>(3, true)); for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++) { int f; cin >> f; if (!f) continue; for (int _y = 0; _y < 3; _y++) for (int _x = 0; _x < 3; _x++) if (x - _x >= 0 && x - _x < 2 && y - _y >= 0 && y - _y < 2) v[_y][_x] = false; } vector< vector<bool> > _dp(9, vector<bool>(4096)); for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) { if (!v[y][x]) continue; for (int a = 0; a < 7; a++) for (int b = 0; b < 7; b++) for (int c = 0; c < 7; c++) for (int d = 0; d < 7; d++) { int i = enc(x, y), j = _enc(a, b, c, d); if (!dp[i][j]) continue; for (int _y = 0; _y < 3; _y++) for (int _x = 0; _x < 3; _x++) { if (_x != x && _y != y) continue; int _a = (_x == 0 && _y == 0 ? 0 : a + 1); int _b = (_x == 2 && _y == 0 ? 0 : b + 1); int _c = (_x == 0 && _y == 2 ? 0 : c + 1); int _d = (_x == 2 && _y == 2 ? 0 : d + 1); int _i = enc(_x, _y), _j = _enc(_a, _b, _c, _d); _dp[_i][_j] = true; } } } dp = _dp; /* int ok = 100; for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) for (int a = 0; a < 7; a++) for (int b = 0; b < 7; b++) for (int c = 0; c < 7; c++) for (int d = 0; d < 7; d++) { int i = enc(x, y), j = _enc(a, b, c, d); if (dp[i][j]) ok = min(ok, a); } cout << ok << endl; */ } bool ok = false; for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) for (int a = 0; a < 7; a++) for (int b = 0; b < 7; b++) for (int c = 0; c < 7; c++) for (int d = 0; d < 7; d++) { int i = enc(x, y), j = _enc(a, b, c, d); if (dp[i][j]) ok = true; } cout << (ok ? 1 : 0) << endl; } }
#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;} int n; int f[366]; int cl[3][3]={}; int dx[9]={0,1,2,-1,-2,0,0,0,0}; int dy[9]={0,0,0,0,0,1,2,-1,-2}; #define IN(x,y) (0<=x && x<=2 && 0<=y && y<=2) int dp[366][3][3][7][7][7][7]; int dfs(int d, int y, int x, int lu, int ru, int lb, int rb) { if(d==n) return 1; if(dp[d][y][x][lu][ru][lb][rb]>=0) return dp[d][y][x][lu][ru][lb][rb]; if(f[d]&cl[y][x]) return 0; int ret = 0; rep(i,9) { int ny = y+dy[i], nx = x+dx[i]; if(!IN(nx,ny)) continue; int nlu = lu-1; int nru = ru-1; int nlb = lb-1; int nrb = rb-1; if(nx==0 && ny==0) nlu=6; if(nx==2 && ny==0) nru=6; if(nx==0 && ny==2) nlb=6; if(nx==2 && ny==2) nrb=6; if(nlu<0 || nru<0 || nlb<0 || nrb<0) continue; ret |= dfs(d+1,ny,nx,nlu,nru,nlb,nrb); } return dp[d][y][x][lu][ru][lb][rb] = ret; } int main() { rep(y,3)rep(x,3) { int p = 4*y+x; for(const auto &a:vector<int>({0,1,4,5})) cl[y][x] |= 1<<(p+a); } while(scanf(" %d", &n),n) { rep(i,n) { f[i]=0; rep(j,16) { int a; scanf(" %d", &a); f[i] |= a<<j; } } memset(dp,-1,sizeof(dp)); printf("%d\n", dfs(0,1,1,5,5,5,5)); } return 0; }
#include <iostream> #include <cmath> #define state(a, b, c, d) (a * 7*7*7 + b * 7*7 + c * 7 + d) using namespace std; int N; const int ST = state(6, 6, 6, 6) + 1; int sche[370][16]; bool dp[370][ST][3][3]; int main(void) { while(1){ cin >> N; if(N == 0) break; for(int i = 1; i <= N; i++){ for(int j = 0; j < 16; j++){ cin >> sche[i][j]; } } if(sche[1][5] || sche[1][6] || sche[1][9] || sche[1][10]){ cout << "0" << endl; continue; } for(int i = 1; i <= N; i++){ for(int j = 0; j < ST; j++){ for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ dp[i][j][x][y] = false; } } } } dp[1][state(1, 1, 1, 1)][1][1] = true; int pos; int st, sa, sb, sc, sd; for(int i = 1; i < N; i++){ for(int j = 0; j < ST; j++){ for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ if(dp[i][j][x][y] == false) continue; for(int nx = 0; nx < 3; nx++){ for(int ny = 0; ny < 3; ny++){ if( abs(nx-x)+abs(ny-y) > 2 || (nx-x)*(ny-y)) continue; pos = ny*4 + nx; if(sche[i+1][pos] || sche[i+1][pos+1] || sche[i+1][pos+4] || sche[i+1][pos+5]) continue; st = j; sd = st % 7, st /= 7; sc = st % 7, st /= 7; sb = st % 7, st /= 7; sa = st % 7; sa++, sb++, sc++, sd++; if(pos == 0) sa = 0; if(pos == 2) sb = 0; if(pos == 8) sc = 0; if(pos == 10) sd = 0; if(sa > 6 || sb > 6 || sc > 6 || sd > 6) continue; dp[i+1][state(sa, sb, sc, sd)][nx][ny] = true; } } } } } } int flag = false; for(int i = 0; i < ST; i++){ for(int x = 0; x < 3; x++){ for(int y = 0; y < 3; y++){ if(dp[N][i][x][y]){ flag = true; goto end; } } } } end:; if(flag) cout << "1" << endl; else cout << "0" << endl; } return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <complex> #include <queue> #include <map> #include <set> #include <cstring> #include <cstdlib> #include <string> #include <cmath> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1<<29; int n; int ba[365][4][4]; bool memo[365][3][3][7*7*7*7]; bool visited[365][3][3][7*7*7*7]; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int hogex[] = {0, 0, 2, 2}; int hogey[] = {0, 2, 0, 2}; bool solve(int day, int y, int x, int S) { if (visited[day][y][x][S]) return memo[day][y][x][S]; visited[day][y][x][S] = 1; REP(i, 2) REP(j, 2) if (ba[day][y+i][x+j]) return memo[day][y][x][S]=0; if (day == n-1) return memo[day][y][x][S]=1; // Žl‹÷‚̏î•ñŒvŽZ int tmp = S; int norain[4]; REP(i,4) { norain[i] = tmp%7; tmp /= 7; } // ŽŸ‚Ì“ú‚Ö REP(i, 4) { int xx = x, yy = y; if (i) { xx+=dx[i]; yy+=dy[i]; } while(0<=xx&&xx<=2&&0<=yy&&yy<=2) { int nextS = 0; int tmp = 1; bool f = 0; REP(j, 4) { if (!(xx==hogex[j] && yy==hogey[j])) { if (norain[j] == 6) f = 1; nextS += tmp * (norain[j] + 1); } tmp *= 7; } if (!f && solve(day+1, yy, xx, nextS)) return memo[day][y][x][S]=1; yy += dy[i]; xx += dx[i]; } } return memo[day][y][x][S]=0; } int main() { while(cin>>n,n) { REP(i, n) REP(j,4) REP(k,4) cin >> ba[i][j][k]; memset(visited,0,sizeof(visited)); cout << (solve(0, 1, 1, 400)? 1: 0) << endl; } }
#include <bits/stdc++.h> using namespace std; #define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++) #define REP(i,n) FOR(i,0,n) #define ALL(a) a.begin(), a.end() #define MS(m,v) memset(m,v,sizeof(m)) #define D10 fixed<<setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ int n; vector<vi> v; short dp[366][10][8][8][8][8]; vi move(int t) { vi res; if (t == 0) res = { 0,1,2,3,6 }; if (t == 1) res = { 0,1,2,4,7 }; if (t == 2) res = { 0,1,2,5,8 }; if (t == 3) res = { 3,4,5,0,6 }; if (t == 4) res = { 3,4,5,1,7 }; if (t == 5) res = { 3,4,5,2,8 }; if (t == 6) res = { 6,7,8,0,3 }; if (t == 7) res = { 6,7,8,1,4 }; if (t == 8) res = { 6,7,8,2,5 }; return res; } vi range(int t) { vi res; if(t<3) res = { t, t + 1,t + 4,t + 5 }; else if (t < 6) res = { t + 1,t + 2,t + 5,t + 6 }; else res = { t + 2,t + 3,t + 6,t + 7 }; return res; } short solve(const int day, const int pos, const int ul, const int ur, const int dl, const int dr) { if (dp[day][pos][ul][ur][dl][dr] >= 0) return dp[day][pos][ul][ur][dl][dr]; if (ul == 7 || ur == 7 || dl == 7 || dr == 7) return 0; if (day == n - 1) return 1; short res = 0; vi can = move(pos); REP(i, can.size()) { int npos = can[i]; vi rain = range(npos); bool f = false; REP(j, rain.size()) if (v[day + 1][rain[j]]) f = true; if (f) continue; int nul = ul + 1, nur = ur + 1, ndl = dl + 1, ndr = dr + 1; if (npos == 0) nul = 0; if (npos == 2) nur = 0; if (npos == 6) ndl = 0; if (npos == 8) ndr = 0; if (solve(day + 1, npos, nul, nur, ndl, ndr)) res = 1; } return dp[day][pos][ul][ur][dl][dr] = res; } int main() { while (cin >> n, n) { MS(dp, -1); v.clear(); v.resize(n, vi(16)); REP(i, n)REP(j, 16) cin >> v[i][j]; bool f = true; vi a = { 5,6,9,10 }; REP(i, 4) if (v[0][a[i]]) f = false; if (!f) puts("0"); else puts(solve(0, 4, 1, 1, 1, 1) == 1 ? "1" : "0"); } return 0; }
#include <iostream> #include <vector> #include <set> using Bool = bool; using Int = long long int; template <class T> using Vector = std::vector<T>; template <class T> using Set = std::set<T>; constexpr Int L = 531441; // 9^6 constexpr Int F = 4; // [4, 0, 0, 0, 0, 0] const Vector<Int> mask{ 0b1100110000000000, 0b0110011000000000, 0b0011001100000000, 0b0000110011000000, 0b0000011001100000, 0b0000001100110000, 0b0000000011001100, 0b0000000001100110, 0b0000000000110011, }; Vector<Int> wet; void init() { wet.assign(L, 0); for (Int b = 0; b < L; ++b) { { Int c = b; for (Int j = 0; j < 6; ++j) { wet[b] |= mask[c % 9]; c /= 9; } } } } Bool solve() { Int n; std::cin >> n; if (n == 0) return false; Set<Int> pos, npos; pos.clear(); pos.insert(F); for (Int q = 0; q < n; ++q) { Int req = 0; for (Int j = 0; j < 16; ++j) { Int b; std::cin >> b; req = (req << 1) + b; } npos.clear(); for (Int p = 0; p < 9; ++p) { if (q == 0 && p != 4) continue; if (req & mask[p]) continue; for (Int b : pos) { if (q >= 6 && (wet[b] | mask[p]) != ((1 << 16) - 1)) continue; Int prev = b % 9; if (p / 3 != prev / 3 && p % 3 != prev % 3) continue; npos.insert((b * 9 + p) % L); } } std::swap(pos, npos); } std::cout << !pos.empty() << std::endl; return true; } int main() { init(); while (solve()) {} return 0; }
#include <cstdio> #include <iostream> #include <set> #include <vector> #include <cstring> #include <algorithm> using namespace std; typedef long long int ll; struct Elem { int pos, day; vector<int> sunny; bool operator<(const Elem &x) const { if(day != x.day) return day < x.day; if(pos != x.pos) return pos < x.pos; return sunny < x.sunny; } }; // generate binary string (not less than k-digit) string to_binString(int n, int k) { string ret = ""; while(n) ret += ('0' + (n&1)), n >>= 1; while(ret.length() < k) ret += '0'; // reverse(ret.begin(), ret.end()); return ret; } int N, val[400]; set<Elem> S; int d[9][4] = { // URDL {-4, -3, 0, 1}, {1, 2, 5, 6}, {4, 5, 8, 9}, {-1, 0, 3, 4}, {-8, -7, -4, -3}, {2, 3, 6, 7}, {8, 9, 12, 13}, {-2, -1, 2, 3}, {0, 1, 4, 5} }; bool ans = false; void solve(Elem e) { if(!S.count(e)) { S.insert(e); if(e.day == N) { ans = true; return; } if(ans) return; for(int k=0; k<9; k++) { if(k == 0 && e.pos/4 == 0) continue; if(k == 1 && e.pos%4 == 2) continue; if(k == 2 && e.pos/4 == 2) continue; if(k == 3 && e.pos%4 == 0) continue; if(k == 4 && e.pos/4 < 2) continue; if(k == 5 && e.pos%4 != 0) continue; if(k == 6 && e.pos/4 != 0) continue; if(k == 7 && e.pos%4 < 2) continue; bool ok = true; int nbit = (1 << 16) - 1, npos = e.pos + d[k][0]; for(int x=0; x<4; x++) { int c = d[k][x] + e.pos; nbit ^= (1 << c); } /* printf("day = %d, dir = %d, bit = \n", e.day, k); for(int i=0; i<4; i++) { for(int j=0; j<4; j++) printf("%d", val[e.day]>>(i*4+j)&1); printf(" "); for(int j=0; j<4; j++) printf("%d", (nbit >> (i*4+j) & 1)); printf("\n"); } printf("\n"); */ if((nbit & val[e.day]) != val[e.day]) continue; vector<int> nxt_vec = e.sunny; for(int i=0; i<16; i++) { if(nbit >> i & 1) { if(nxt_vec[i] + 1 > 6) ok = false; nxt_vec[i]++; } else { nxt_vec[i] = 0; } } /* for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { printf("%d", nxt_vec[i*4+j]); } printf("\n"); } */ if(ok) { Elem nxt{npos, e.day+1, nxt_vec}; solve(nxt); } } } } int main() { while(1) { S.clear(); scanf("%d", &N); if(!N) break; memset(val, 0, sizeof(val)); for(int i=0; i<N; i++) { for(int j=0; j<16; j++) { int p; scanf("%d", &p); // printf("%d", p); // if(j%4 == 3) printf("\n"); val[i] = (val[i] << 1) | p; } } // 初日の判定 vector<int> vec(16, 1); int bit = (1 << 16) - 1, pos = 5; for(int i=0; i<4; i++) { int c = d[8][i] + pos; bit ^= (1 << c); vec[c] = 0; } if((bit & val[0]) != val[0]) { printf("0\n"); continue; } ans = false; Elem s{pos, 1, vec}; solve(s); printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<map> #include<cstring> #include<string> #include <math.h> #include<algorithm> // #include <boost/multiprecision/cpp_int.hpp> #include<functional> #define int long long #define inf 1000000007 #define pa pair<int,int> #define ll long long #define pal pair<double,double> #define ppap pair<pa,int> #define ssa pair<string,int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) int dx[4]={0,-1,0,1}; int dy[4]={1,0,-1,0}; using namespace std; class pa3{ public: int x,y,z; pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} bool operator < (const pa3 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; return z<p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa3 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; return z>p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa3 &p) const{ return x==p.x && y==p.y && z==p.z; } bool operator != (const pa3 &p) const{ return !( x==p.x && y==p.y && z==p.z); } }; class pa4{ public: double x; int y,z,w; pa4(double x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} bool operator < (const pa4 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; if(z!=p.z)return z<p.z; return w<p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa4 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; if(z!=p.z)return z>p.z; return w>p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa4 &p) const{ return x==p.x && y==p.y && z==p.z &&w==p.w; } }; class pa2{ public: int x,y; pa2(int x=0,int y=0):x(x),y(y) {} pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} bool operator < (const pa2 &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa2 &p) const{ return x != p.x ? x>p.x: y>p.y; } bool operator == (const pa2 &p) const{ return abs(x-p.x)==0 && abs(y-p.y)==0; } bool operator != (const pa2 &p) const{ return !(abs(x-p.x)==0 && abs(y-p.y)==0); } }; #define ppa pair<int,pas> class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } string itos( int i ) { ostringstream s ; s << i ; return s.str() ; } int gcd(int v,int b){ if(v>b) return gcd(b,v); if(v==b) return b; if(b%v==0) return v; return gcd(v,b%v); } double distans(double x1,double y1,double x2,double y2){ double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); return sqrt(rr); } // int pr[2000010]; // int inv[2000010]; int beki(int wa,int rr,int warukazu){ if(rr==0) return 1ll; if(rr==1) return wa%warukazu; if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu; int zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } /* int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ //priority_queue<pa3,vector<pa3>,greater<pa3>> pq; //sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ //vector<double> ans(100000000),ans2(100000000); bool dp[380][9][7][7][7][7]; bool sumi[380][9][7][7][7][7]; int n; vector<int> kanou[9]; int kumo[9]={0}; int day[400]; void junbi(){ kanou[0].pb(0); kanou[0].pb(1); kanou[0].pb(2); kanou[0].pb(3); kanou[0].pb(6); kanou[1].pb(0); kanou[1].pb(1); kanou[1].pb(2); kanou[1].pb(4); kanou[1].pb(7); kanou[2].pb(0); kanou[2].pb(1); kanou[2].pb(2); kanou[2].pb(5); kanou[2].pb(8); kanou[3].pb(0); kanou[3].pb(3); kanou[3].pb(4); kanou[3].pb(5); kanou[3].pb(6); kanou[4].pb(4); kanou[4].pb(1); kanou[4].pb(5); kanou[4].pb(3); kanou[4].pb(7); kanou[5].pb(8); kanou[5].pb(5); kanou[5].pb(2); kanou[5].pb(3); kanou[5].pb(4); kanou[6].pb(0); kanou[6].pb(8); kanou[6].pb(7); kanou[6].pb(3); kanou[6].pb(6); kanou[7].pb(4); kanou[7].pb(1); kanou[7].pb(8); kanou[7].pb(7); kanou[7].pb(6); kanou[8].pb(2); kanou[8].pb(5); kanou[8].pb(8); kanou[8].pb(7); kanou[8].pb(6); kumo[0]=0b0000000000110011; kumo[1]=0b0000000001100110; kumo[2]=0b0000000011001100; kumo[3]=0b0000001100110000; kumo[4]=0b0000011001100000; kumo[5]=0b0000110011000000; kumo[6]=0b0011001100000000; kumo[7]=0b0110011000000000; kumo[8]=0b1100110000000000; } bool dfs(int d,int pos,int r5,int r6,int r9,int r10){ if(r5>=7) return false; if(r6>=7) return false; if(r9>=7) return false; if(r10>=7) return false; if(day[d]& kumo[pos]) return false; //cout<<d<<" "<<pos<<" "<<r5<<" "<<r6<<" "<<r9<<" "<<r10<<endl; if(sumi[d][pos][r5][r6][r9][r10]) return dp[d][pos][r5][r6][r9][r10]; if(d==n){ // cout<<d<<" "<<pos<<endl; return true; } bool bo=false; for(auto v:kanou[pos]){ int l5=r5; int l6=r6; int l9=r9; int l10=r10; if((kumo[v]&(1<<0) )==0) l5++; else l5=0; if((kumo[v]&(1<<3) )==0) l6++; else l6=0; if((kumo[v]&(1<<12) )==0) l9++; else l9=0; if((kumo[v]&(1<<15) )==0) l10++; else l10=0; if(dfs(d+1,v,l5,l6,l9,l10)){ bo=true; goto lll; } } lll:; sumi[d][pos][r5][r6][r9][r10]=1; dp[d][pos][r5][r6][r9][r10]=bo; return bo; } signed main(){ junbi(); int a[110][110]; while(1){ cin>>n; if(n==0) return 0; for(int i=0;i<370;i++)for(int k=0;k<9;k++)for(int l1=0;l1<7;l1++)for(int l2=0;l2<7;l2++)for(int l3=0;l3<7;l3++)for(int l4=0;l4<7;l4++){ sumi[i][k][l1][l2][l3][l4]=0; } for(int i=1;i<=n;i++){ int l=0; for(int j=0;j<16;j++){ char c; cin>>c; if(c=='1') l+= 1<<j; } day[i]=l; } if(dfs(1,4,1,1,1,1))cout<<1<<endl; else cout<<0<<endl; } return 0; }
#include <iostream> #include <algorithm> using namespace std; const int N = 365; int dat[N][16], n; bool vis[N+1][9][7][7][7][7]; int dfs(int d, int p, int k0, int k1, int k2, int k3){ static int dy[4] = {-1, 0, 1, 0}; static int dx[4] = {0, 1, 0, -1}; if(d == n) return 1; int nk0 = k0, nk1 = k1, nk2 = k2, nk3 = k3; if(p == 0) nk0 = 0; if(p == 2) nk1 = 0; if(p == 8) nk2 = 0; if(p == 10) nk3 = 0; if(nk0 >= 7 || nk1 >= 7 || nk2 >= 7 || nk3 >= 7) return 0; if(dat[d][p] == 1 || dat[d][p+1] == 1 || dat[d][p+4] == 1 || dat[d][p+5] == 1) return 0; if(vis[d][p][k0][k1][k2][k3]) return 0; vis[d][p][k0][k1][k2][k3] = 1; for(int j=0;j<3;j++){ for(int i=0;i<4;i++){ int ny = p / 4 + dy[i] * j; int nx = p % 4 + dx[i] * j; if(min(ny, nx) < 0 || max(ny, nx) > 2) continue; if(dfs(d + 1, ny*4 + nx, nk0+1, nk1+1, nk2+1, nk3+1)) return 1; } } return 0; } int main(){ while(cin >> n && n){ for(int i=0;i<n;++i) for(int j=0;j<16;++j) cin >> dat[i][j]; fill(vis[0][0][0][0][0], vis[N+1][0][0][0][0], 0); cout << dfs(0, 5, 1, 1, 1, 1) << endl; } }
#include <iostream> #include <queue> #include <cassert> using namespace std; int S[366][4][4] = {}; int vis[366][3][3][7][7][7][7] = {}; int dx[9] = {-1,0,1,0,-2,0,2,0,0},dy[9] = {0,-1,0,1,0,-2,0,2,0}; struct state{ int x = 1,y = 1; int d = 1; int rest[4][4] = {}; bool in(){ return 0<=x && x<=2 && 0<=y && y<=2; } void update(){ for(int i=0;i<4;i++) for(int j=0;j<4;j++) rest[i][j]++; for(int i=0;i<2;i++) for(int j=0;j<2;j++) rest[x+i][y+j] = 0; } bool ok(){ if(!in()) return false; for(int i=0;i<2;i++) for(int j=0;j<2;j++) if(S[d][x+i][y+j]==1) return false; return true; } bool ok2(){ for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(rest[i][j]==7) return false; return true; } }; int main(){ int N; while(cin >> N && N){ for(int d=1;d<=N;d++) for(int i=0;i<4;i++) for(int j=0;j<4;j++) cin >> S[d][i][j]; for(int d=0;d<=N;d++) for(int i=0;i<3;i++) for(int j=0;j<3;j++) for(int a=0;a<7;a++) for(int b=0;b<7;b++) for(int c=0;c<7;c++) for(int e=0;e<7;e++) vis[d][i][j][a][b][c][e] = 0; queue<state> Q; state init; init.update(); if(!init.ok()){ cout << 0 << endl; continue; } Q.push(init); int complete = 0; while(!Q.empty()){ state now = Q.front(); Q.pop(); if(now.d==N){ complete = 1; break; } for(int i=0;i<9;i++){ state ne = now; ne.x += dx[i]; ne.y += dy[i]; ne.d++; if(!ne.ok()) continue; ne.update(); if(ne.ok2() && !vis[ne.d][ne.x][ne.y][ne.rest[0][0]][ne.rest[0][3]][ne.rest[3][0]][ne.rest[3][3]]){ vis[ne.d][ne.x][ne.y][ne.rest[0][0]][ne.rest[0][3]][ne.rest[3][0]][ne.rest[3][3]] = 1; Q.push(ne); } } } cout << complete << endl; } }
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <complex> #include <queue> #include <map> #include <set> #include <cstring> #include <cstdlib> #include <string> #include <cmath> using namespace std; #define REP(i,n) for(int i=0;i<(int)n;++i) #define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define ALL(c) (c).begin(), (c).end() const int INF = 1<<29; int n; int ba[365][4][4]; bool memo[366][3][3][7*7*7*7]; bool visited[366][3][3][7*7*7*7]; int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; int hogex[] = {0, 0, 2, 2}; int hogey[] = {0, 2, 0, 2}; bool solve(int day, int y, int x, int S) { if (visited[day][y][x][S]) return memo[day][y][x][S]; visited[day][y][x][S] = 1; if (day == n) return memo[day][y][x][S]=1; int tmp = S; int norain[4]; REP(i,4) { norain[i] = tmp%7; tmp /= 7; } REP(i, 2) REP(j, 2) if (ba[day][y+i][x+j]) return memo[day][y][x][S]=0; REP(i, 4) { int xx = x, yy = y; if (i) { xx+=dx[i]; yy+=dy[i]; } while(0<=xx&&xx<=2&&0<=yy&&yy<=2) { int nextS = 0; int tmp = 1; bool f = 0; REP(j, 4) { if (!(xx==hogex[j] && yy==hogey[j])) { if (norain[j] == 6) f = 1; nextS += tmp * (norain[j] + 1); } tmp *= 7; } if ((day==n || !f) && solve(day+1, yy, xx, nextS)) { // printf("%d,%d,%d,%d\n",day,y,x,S); // REP(j, 4) // cout << norain[j] << " "; // cout<< endl; return memo[day][y][x][S]=1; } yy += dy[i]; xx += dx[i]; } } return memo[day][y][x][S]=0; } int main() { while(cin>>n,n) { REP(i, n) { REP(j,4) { REP(k,4) { cin >> ba[i][j][k]; // cout <<ba[i][j][k] << " "; } // cout << endl; } // cout << endl; } memset(visited,0,sizeof(visited)); cout << (solve(0, 1, 1, 400)? 1: 0) << endl; } }
#include <cstdio> #include <cstring> #include <algorithm> #define MOD 531441 using namespace std; int n; int fie[366][4][4]; bool dp[2][81*81*81]; int rx[9]={0,1,2,0,1,2,0,1,2}; int ry[9]={0,0,0,1,1,1,2,2,2}; int data[81*81*81][4][4]; int data2[9][4][4]; bool check(int v,int bit,int next){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ if(data2[next][j][i]==1 && fie[v][j][i]==1)return false; if(v>=6 && data[bit][j][i]+data2[next][j][i]==0)return false; } } return true; } int main(void){ for(int i=0;i<81*81*81;i++){ int ni=i; for(int j=0;j<6;j++){ data[i][rx[ni%9]][ry[ni%9]]=1; data[i][rx[ni%9]+1][ry[ni%9]]=1; data[i][rx[ni%9]][ry[ni%9]+1]=1; data[i][rx[ni%9]+1][ry[ni%9]+1]=1; ni/=9; } } for(int i=0;i<9;i++){ data2[i][rx[i]][ry[i]]=1; data2[i][rx[i]+1][ry[i]]=1; data2[i][rx[i]][ry[i]+1]=1; data2[i][rx[i]+1][ry[i]+1]=1; } while(1){ scanf("%d",&n); if(n==0)break; for(int i=0;i<n;i++){ for(int j=0;j<4;j++){ for(int k=0;k<4;k++){ scanf("%d",&fie[i][k][j]); } } } memset(dp,false,sizeof(dp)); int now=1,prev=0; if(fie[0][1][1]+fie[0][2][1]+fie[0][2][2]+fie[0][1][2]==0)dp[prev][4]=true; for(int i=1;i<n;i++){ for(int j=0;j<81*81*81;j++){ if(dp[prev][j]){ for(int k=0;k<9;k++){ if(rx[j%9]!=rx[k] && ry[j%9]!=ry[k])continue; if(check(i,j,k)){ //printf("%d %d %d\n",i,j,k); dp[now][(j*9+k)%MOD]=true; } } } } swap(now,prev); memset(dp[now],false,sizeof(dp[now])); } int res=0; for(int i=0;i<81*81*81;i++){ if(dp[prev][i])res=1; } printf("%d\n",res); } return 0; }
#include<bits/stdc++.h> using namespace std; struct state{int p,y,x,a,b,c,d;}; int dy[]={-1,0,1,0}; int dx[]={0,1,0,-1}; int n; int t[367][4][4]; bool visited[367][3][3][8][8][8][8]; int solve(){ int K=7; if(t[0][1][1]||t[0][1][2]||t[0][2][1]||t[0][2][2])return 0; memset(visited,0,sizeof(visited)); queue< state > Q; Q.push((state){0,1,1,1,1,1,1}); while(!Q.empty()){ state s=Q.front();Q.pop(); if(s.p==n-1)return 1; for(int i=0;i<4;i++){ for(int j=0;j<=2;j++){ int np=s.p+1; int ny=s.y+dy[i]*j; int nx=s.x+dx[i]*j; if(ny<0 || 2<ny)continue; if(nx<0 || 2<nx)continue; if(t[np][ny][nx])continue; if(t[np][ny+1][nx])continue; if(t[np][ny][nx+1])continue; if(t[np][ny+1][nx+1])continue; int na=s.a+1,nb=s.b+1,nc=s.c+1,nd=s.d+1; if(ny==0&&nx==0)na=0; else if(ny==0&&nx==2)nb=0; else if(ny==2&&nx==0)nc=0; else if(ny==2&&nx==2)nd=0; if(na==K||nb==K||nc==K||nd==K)continue; if(visited[np][ny][nx][na][nb][nc][nd])continue; visited[np][ny][nx][na][nb][nc][nd]=true; Q.push((state){np,ny,nx,na,nb,nc,nd}); } } } return 0; } int main(){ while(1){ cin>>n; if(n==0)break; memset(t,0,sizeof(t)); for(int i=0;i<n;i++) for(int j=0;j<4;j++) for(int k=0;k<4;k++) cin>>t[i][j][k]; cout<<solve()<<endl; } return 0; }
//include //------------------------------------------ #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 <climits> #include <queue> using namespace std; //typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; //container util //------------------------------------------ #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define EXIST(s,e) ((s).find(e)!=(s).end()) #define SORT(c) sort((c).begin(),(c).end()) //repetition //------------------------------------------ #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) //constant //-------------------------------------------- const double EPS = 1e-10; const double PI = acos(-1.0); bool dp[2][9][7*7*7*7]; int main(){ cin.tie(0); ios_base::sync_with_stdio(false); int N; while(cin>>N,N){ int crt = 0, nxt = 1; dp[crt][4][0] = true; FOR(i,1,N+1){ REP(j,9) REP(k,7*7*7*7) dp[nxt][j][k] = false; VI vi(16); REP(j,16) cin >> vi[j]; for(int idx=0;idx<9;++idx){ for(int c=0;c<7*7*7*7;++c){ if(!dp[crt][idx][c]) continue; set<int> p; for(int x=idx/3*3;x<=idx/3*3+2;++x) p.insert(x); for(int y=idx%3;y<9;y+=3) p.insert(y); VI ps_(4); int tmp = c; REP(j,4){ ps_[j] = tmp % 7; tmp /= 7; } for(int idx_: p){ if(i == 1 && idx_ != 4) continue; int orig; if(idx_ < 3) orig = idx_; else if(idx_ < 6) orig = 1 + idx_; else orig = 2 + idx_; if(vi[orig] || vi[orig+1] || vi[orig+4] || vi[orig+5]) continue; VI ps = ps_; if(idx_ != 0) ++ps[0]; else ps[0] = 0; if(idx_ != 2) ++ps[1]; else ps[1] = 0; if(idx_ != 6) ++ps[2]; else ps[2] = 0; if(idx_ != 8) ++ps[3]; else ps[3] = 0; if(ps[0] >= 7 || ps[1] >= 7 || ps[2] >= 7 || ps[3] >= 7) continue; tmp = 0; REP(j,4) tmp = tmp*7 + ps[3-j]; dp[nxt][idx_][tmp] = true; } } } swap(crt, nxt); } bool ok = false; for(int idx=0;idx<9;++idx) for(int c=0;c<7*7*7*7;++c) ok = ok || dp[crt][idx][c]; cout << (ok? 1: 0) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; //#define int long long #define reps(i,s,n) for(int (i)=(s);(i)<(n);++(i)) #define rep(i,n) reps(i,0,n) #define rept(i,n) rep(i,(n)+1) #define repst(i,s,n) reps(i,s,(n)+1) #define reprt(i,n,t) for(int (i)=(n);(i)>=(t);--(i)) #define repr(i,n) reprt(i,n,0) #define each(itr,v) for(auto &(itr):(v)) #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define pb push_back #define mp make_pair #define fi first #define se second #define tmax(x,y,z) max(x,max(y,z)) #define tmin(x,y,z) min(x,min(y,z)) #define chmin(x,y) x=min(x,y) #define chmax(x,y) x=max(x,y) #define ln '\n' #define bln(i,n) (((i)==(n)-1)?'\n':' ') #define dbg(x) cout<<#x" = "<<(x)<<ln<<flush #define dbga(x,n) {cout<<#x" : ";for(int (i)=0;i<(n);++i){cout<<((x)[i])<<(i==((n)-1)?'\n':' ')<<flush;}} #define zero(a) memset(a,0,sizeof(a)) #define unq(a) sort(all(a)),a.erase(unique(all(a)),a.end()) typedef complex<double> P; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<string> vst; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vector<int> > mat; const ll inf = (ll)1e9+10; const ll linf = (ll)1e18+10; const ll mod = (ll)(1e9+7); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1}; const double eps = 1e-10; ll mop(ll a,ll b,ll m=mod) {ll r=1;a%=m;for(;b;b>>=1){if(b&1)r=r*a%m;a=a*a%m;}return r;} ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) {return a*b/gcd(a,b);} bool ool(int x,int y,int h,int w) {return((x<0)||(h<=x)||(y<0)||(w<=y));} bool deq(double a,double b) {return abs(a-b)<eps;} struct oreno_initializer { oreno_initializer() { cin.tie(0); ios::sync_with_stdio(0); } } oreno_initializer; // ━━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥… // .。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+.。.:( ^ω^)・゚+ // ・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・‥…━━━☆・ // 7日以上連続で雨が降らない点があってはいけない // i日目は1の点に雨を降らせてはいけない // 毎朝雨エリアは上下左右どれか1方向に1/2マス動かすかその場に留まるか // 何日雨が降ってないかは4隅(雲座標0,2,6,8)だけ記録すればいい 7^4=2401 // 現在の雲の位置は9通り、最大365日 全部掛けると8×10^6くらい // dp[i][j][k][l][m][n]: i日目に雲がjにあり4隅をそれぞれk〜n日放置してる状態があり得るかどうか // 遷移に時間かけると間に合わないかも int n, x[400], t; bool dp[2][9][7][7][7][7]; vi e[9]; set<int> in[9]; signed main() { rep(i,9) { e[i].pb(i); t = i/3*3; rep(yo,3) if (i!=t+yo) e[i].pb(t+yo); t = i%3; rep(ta,3) if (i!=t+ta*3) e[i].pb(t+ta*3); t = i + i/3; rep(j,2) rep(k,2) in[i].insert(t+j*4+k); } while (1) { cin >> n; if (n==0) break; zero(x); rep(i,n) rep(j,16) { cin >> t; x[i] |= (t<<j); } zero(dp[0]); bool ng = 0; each(p,in[4]) if (x[0]&1<<p) ng = 1; dp[1][4][1][1][1][1] = 1-ng; reps(i,1,n) { int fr = i&1, to = (i+1)&1; zero(dp[to]); rep(j,9) rep(a,7) rep(b,7) rep(c,7) rep(d,7) if (dp[fr][j][a][b][c][d]) each(u,e[j]){ ng = 0; each(p,in[u]) if (x[i]&1<<p) ng = 1; if (ng) continue; int aa = a+1, bb = b+1, cc = c+1, dd = d+1; if (u==0) aa = 0; else if (u==2) bb = 0; else if (u==6) cc = 0; else if (u==8) dd = 0; if (aa<7 && bb<7 && cc<7 && dd<7) dp[to][u][aa][bb][cc][dd] = 1; } } bool res = 0; rep(j,9) rep(a,7) rep(b,7) rep(c,7) rep(d,7) res |= dp[n&1][j][a][b][c][d]; cout << res << ln; } }
#include<stdio.h> bool dp[366][3][3][8][8][8][8]; int dat[365][16]; int dame[365][3][3]; int main(){ int a; while(scanf("%d",&a),a){ for(int i=0;i<a;i++) for(int j=0;j<16;j++) scanf("%d",&dat[i][j]); for(int i=0;i<a;i++) for(int j=0;j<3;j++) for(int jj=0;jj<3;jj++) for(int k=0;k<7;k++) for(int l=0;l<7;l++) for(int m=0;m<7;m++) for(int n=0;n<7;n++) dp[i][j][jj][k][l][m][n]=false; for(int i=0;i<365;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++) dame[i][j][k]=0; for(int i=0;i<a;i++){ for(int j=0;j<3;j++) for(int k=0;k<3;k++) for(int l=0;l<2;l++) for(int m=0;m<2;m++) if(dat[i][(j+l)*4+k+m])dame[i][j][k]=1; } if(dat[0][5]|dat[0][6]|dat[0][10]|dat[0][9]); else dp[0][1][1][1][1][1][1]=true; for(int i=0;i<a-1;i++){ for(int r=0;r<3;r++){ for(int c=0;c<3;c++){ for(int k=0;k<7;k++){ for(int l=0;l<7;l++){ for(int m=0;m<7;m++){ for(int n=0;n<7;n++){ if(!dp[i][r][c][k][l][m][n])continue; //printf("%d %d %d %d %d %d %d\n",i,r,c,k,l,m,n); if(r&&!dame[i+1][r-1][c]){ int R=r-1; if(R==0&&c==0&&l<6&&m<6&&n<6)dp[i+1][R][c][0][l+1][m+1][n+1]=true; else if(R==0&&c==2&&k<6&&m<6&&n<6)dp[i+1][R][c][k+1][0][m+1][n+1]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][R][c][k+1][l+1][m+1][n+1]=true; } if(c&&!dame[i+1][r][c-1]){ int C=c-1; if(r==0&&C==0&&l<6&&m<6&&n<6)dp[i+1][r][C][0][l+1][m+1][n+1]=true; else if(r==2&&C==0&&k<6&&l<6&&n<6)dp[i+1][r][C][k+1][l+1][0][n+1]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][r][C][k+1][l+1][m+1][n+1]=true; } if(r-1>0&&!dame[i+1][r-2][c]){ int R=r-2; if(R==0&&c==0&&l<6&&m<6&&n<6)dp[i+1][R][c][0][l+1][m+1][n+1]=true; else if(R==0&&c==2&&k<6&&m<6&&n<6)dp[i+1][R][c][k+1][0][m+1][n+1]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][R][c][k+1][l+1][m+1][n+1]=true; } if(c-1>0&&!dame[i+1][r][c-2]){ int C=c-2; if(r==0&&C==0&&l<6&&m<6&&n<6)dp[i+1][r][C][0][l+1][m+1][n+1]=true; else if(r==2&&C==0&&k<6&&l<6&&n<6)dp[i+1][r][C][k+1][l+1][0][n+1]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][r][C][k+1][l+1][m+1][n+1]=true; } if(r<2&&!dame[i+1][r+1][c]){ int R=r+1; if(R==2&&c==0&&k<6&&l<6&&n<6)dp[i+1][R][c][k+1][l+1][0][n+1]=true; else if(R==2&&c==2&&k<6&&l<6&&m<6)dp[i+1][R][c][k+1][l+1][m+1][0]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][R][c][k+1][l+1][m+1][n+1]=true; } if(c<2&&!dame[i+1][r][c+1]){ int C=c+1; if(r==0&&C==2&&k<6&&m<6&&n<6)dp[i+1][r][C][k+1][0][m+1][n+1]=true; else if(r==2&&C==2&&k<6&&l<6&&m<6)dp[i+1][r][C][k+1][l+1][m+1][0]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][r][C][k+1][l+1][m+1][n+1]=true; } if(r<1&&!dame[i+1][r+2][c]){ int R=r+2; if(R==2&&c==0&&k<6&&l<6&&n<6)dp[i+1][R][c][k+1][l+1][0][n+1]=true; else if(R==2&&c==2&&k<6&&l<6&&m<6)dp[i+1][R][c][k+1][l+1][m+1][0]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][R][c][k+1][l+1][m+1][n+1]=true; } if(c<1&&!dame[i+1][r][c+2]){ int C=c+2; if(r==0&&C==2&&k<6&&m<6&&n<6)dp[i+1][r][C][k+1][0][m+1][n+1]=true; else if(r==2&&C==2&&k<6&&l<6&&m<6)dp[i+1][r][C][k+1][l+1][m+1][0]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][r][C][k+1][l+1][m+1][n+1]=true; } if(!dame[i+1][r][c]){ if(r==0&&c==0&&l<6&&m<6&&n<6)dp[i+1][r][c][0][l+1][m+1][n+1]=true; else if(r==0&&c==2&&k<6&&m<6&&n<6)dp[i+1][r][c][k+1][0][m+1][n+1]=true; else if(r==2&&c==0&&k<6&&l<6&&n<6)dp[i+1][r][c][k+1][l+1][0][n+1]=true; else if(r==2&&c==2&&k<6&&l<6&&m<6)dp[i+1][r][c][k+1][l+1][m+1][0]=true; else if(k<6&&l<6&&m<6&&n<6)dp[i+1][r][c][k+1][l+1][m+1][n+1]=true; } } } } } } } } bool ret=false; for(int i=0;i<3;i++) for(int j=0;j<3;j++) for(int k=0;k<7;k++) for(int l=0;l<7;l++) for(int m=0;m<7;m++) for(int n=0;n<7;n++) if(dp[a-1][i][j][k][l][m][n])ret=true; if(ret)printf("1\n"); else printf("0\n"); } }
#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 dy[] = {0, 2, 1, 0, 0, -2, -1, 0, 0}; int dx[] = {0, 0, 0, 2, 1, 0, 0, -2, -1}; int main() { for(;;){ int n; cin >> n; if(n == 0) return 0; bool first = true; vector<vector<vector<bool> > > dp(3, vector<vector<bool> >(3, vector<bool>(7*7*7*7, false))); dp[1][1][7*7*7*7-1] = true; while(--n >= 0){ vector<vector<vector<bool> > > nextDp(3, vector<vector<bool> >(3, vector<bool>(7*7*7*7, false))); vector<vector<int> > market(4, vector<int>(4)); for(int i=0; i<4; ++i){ for(int j=0; j<4; ++j){ cin >> market[i][j]; } } for(int y0=0; y0<3; ++y0){ for(int x0=0; x0<3; ++x0){ for(int a=0; a<7*7*7*7; ++a){ if(!dp[y0][x0][a]) continue; vector<int> rest(4); int tmp = a; for(int i=0; i<4; ++i){ rest[i] = tmp % 7; tmp /= 7; } for(int i=0; i<9; ++i){ if(first && i > 0) break; int y = y0 + dy[i]; int x = x0 + dx[i]; if(y < 0 || y > 2 || x < 0 || x > 2) continue; if(market[y][x] || market[y][x+1] || market[y+1][x] || market[y+1][x+1]) continue; vector<int> nextRest = rest; if(y == 0 && x == 0) nextRest[0] = 7; if(y == 0 && x == 2) nextRest[1] = 7; if(y == 2 && x == 0) nextRest[2] = 7; if(y == 2 && x == 2) nextRest[3] = 7; bool ng = false; for(int i=0; i<4; ++i){ -- nextRest[i]; if(nextRest[i] < 0) ng = true; } if(!ng){ int b = 0; for(int i=3; i>=0; --i){ b *= 7; b += nextRest[i]; } nextDp[y][x][b] = true; } } } } } dp.swap(nextDp); first = false; } bool ret = false; for(int i=0; i<3; ++i){ for(int j=0; j<3; ++j){ for(int k=0; k<7*7*7*7; ++k){ ret |= dp[i][j][k]; } } } if(ret) cout << 1 << endl; else cout << 0 << endl; } }
#include<bits/stdc++.h> #define range(i,a,b) for(int i = (a); i < (b); i++) #define rep(i,b) for(int i = 0; i < (b); i++) #define all(a) (a).begin(), (a).end() #define show(x) cerr << #x << " = " << (x) << endl; //const int INF = 1e8; using namespace std; #define int long long bool getBit(int num, int i){ return ((num & (1 << i)) != 0); } int setBit(int num, int i){ if(i < 0) return num; return num | (1 << i); } int toInteger(int r[4][4]){ int k = 1; int res = 0; rep(i,4){ rep(j,4){ res += r[i][j] * k; k *= 10; } } return res; } void toRow(int num, int r[4][4]){ rep(i,4){ rep(j,4){ r[i][j] = num % 10; num /= 10; } } } const int dy[16] = { 0,-1, 0, 1, 0,-2, 0, 2, 0 }; const int dx[16] = { 1, 0,-1, 0, 2, 0,-2, 0, 0 }; int n; bool fes[366][4][4]; set<pair<int,pair<int,int>>> m; bool check(long long& rain, int ty, int tx ,int day){ int r[4][4] = {{0}}; toRow(rain, r); assert(rain == toInteger(r)); rep(i,4){//??¨????????´??????+1?????? rep(j,4){ r[i][j]++; } } range(i,ty, ty + 2){ //??¨??????????????¨?????????0????????? range(j,tx, tx + 2){ r[i][j] = 0; if(fes[day][i][j]) return false; //??¨?????????????????¨?????§?????????????????????????????? } } rep(i,4){//???????????§???7??????????????\?????????false rep(j,4){ if(r[i][j] >= 7) return false; } } rain = toInteger(r); return true; } bool dfs(long long rain, int y, int x, int day){ if(m.count(make_pair(rain,make_pair(y * 4 + x,day)))) return false; m.emplace(make_pair(rain,make_pair(y * 4 + x,day))); if(not check(rain, y, x, day)) return false; if(day == n){ return true; } bool res = false; rep(i,9){ int ny = y + dy[i]; int nx = x + dx[i]; if(ny < 0 || ny >= 3 || nx < 0 || nx >= 3) continue; res |= dfs(rain, ny, nx, day + 1); } return res; } signed main(){ while(cin >> n,n){ m.clear(); memset(fes, 0, sizeof(fes)); rep(i,n){ rep(j,4){ rep(k,4){ cin >> fes[i][j][k]; } } } cout << dfs(0, 1, 1, 0) << endl; } }
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cctype> using namespace std; typedef long long ll; #define REP(i,n,m) for(int i=n;i<m;i++) #define rep(i,n) REP(i,0,n) class State{ public: int x,y; int day; int a,b,c,d; State(){} State(int _x,int _y,int _day,int _a,int _b,int _c,int _d){ x = _x; y = _y; day = _day; a = _a; b = _b; c = _c; d = _d; } }; int n; int dx[] = {0, 1,-1, 0}; int dy[] = {1, 0, 0,-1}; bool t[400][4][4]; bool closed[4][4][400][7][7][7][7]; bool check(State st){ if(st.a == 7 || st.b == 7 || st.c == 7 || st.d == 7){ return false; } for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ if(t[st.day][st.y+i][st.x+j]){ return false; } } } return true; } int solve(){ queue<State> open; memset(closed,0,sizeof(closed)); /* rep(i,3){ rep(j,3){ int a = (i == 0 && j == 0 ? 0 : 1); int b = (i == 0 && j == 2 ? 0 : 1); int c = (i == 2 && j == 0 ? 0 : 1); int d = (i == 2 && j == 2 ? 0 : 1); open.push(State(j,i,0,a,b,c,d)); } } */ open.push(State(1,1,0,1,1,1,1)); while(!open.empty()){ State st = open.front(); open.pop(); if(!check(st)){ continue; } if(closed[st.y][st.x][st.day][st.a][st.b][st.c][st.d]){ continue; } closed[st.y][st.x][st.day][st.a][st.b][st.c][st.d] = true; if(st.day == n){ return 1; } rep(i,4){ rep(j,3){ int nx = st.x + j * dx[i]; int ny = st.y + j * dy[i]; if(nx < 0 || 2 < nx || ny < 0 || 2 < ny){ continue; } int na = (nx == 0 && ny == 0 ? 0 : st.a + 1); int nb = (nx == 2 && ny == 0 ? 0 : st.b + 1); int nc = (nx == 0 && ny == 2 ? 0 : st.c + 1); int nd = (nx == 2 && ny == 2 ? 0 : st.d + 1); open.push(State(nx,ny,st.day+1,na,nb,nc,nd)); } } } return 0; } int main(){ while(cin>>n,n){ rep(i,n){ rep(j,4){ rep(k,4){ cin>>t[i][j][k]; } } } cout<<solve()<<endl; } }
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; typedef pair<ld, ld> LDP; //i日目で、雲がj番目で、最後に〇〇を訪れたのは〇日前 int memo[365][9][7][7][7][7]; int dx[4] = { 0,0,1,1 }; int dy[4] = { 0,1,0,1 }; void init() { rep(i, 365) { rep(j, 9) { rep(k, 7) { rep(l, 7) { rep(m,7) { rep(n,7) { memo[i][j][k][l][m][n] = 0; } } } } } } } bool check(int k, int a[16]) { int x = k / 3; int y = k % 3; rep(i, 4) { int nx = x + dx[i]; int ny = y + dy[i]; if (a[nx*4+ny])return false; } return true; } int rained(int k) { if (k == 0)return 1; else if (k == 2)return 2; else if (k == 6)return 3; else if (k == 8)return 4; return 0; } bool validroute(int i, int j) { int x1 = i / 3; int x2 = j / 3; int y1 = i % 3; int y2 = j % 3; if (x1 == x2 || y1 == y2)return true; return false; } int main(){ int n; while (cin >> n, n) { init(); int a[16]; rep(i, 16) { cin >> a[i]; } bool pass = false; if (!check(4, a)) { cout << 0 << endl; pass = true; } memo[0][4][1][1][1][1] = 1; rep(i, n - 1) { rep(j, 16) { cin >> a[j]; } if (pass)continue; rep(j, 9) { rep(k, 9) { if (!validroute(j, k))continue; if (!check(k, a))continue; rep(k1, 7) { rep(k2, 7) { rep(k3, 7) { rep(k4, 7) { if (!memo[i][j][k1][k2][k3][k4])continue; int nex[4] = { k1 + 1,k2 + 1,k3 + 1,k4 + 1 }; int ch = rained(k); if (ch > 0) { nex[ch - 1] = 0; } bool cannex = true; rep(aa, 4) { if (nex[aa] == 7)cannex = false; } if (cannex) { memo[i+1][k][nex[0]][nex[1]][nex[2]][nex[3]] = 1; } } } } } } } } if (pass)continue; int ans = 0; rep(j, 9) { rep(k1, 7) { rep(k2, 7) { rep(k3, 7) { rep(k4, 7) { if (memo[n - 1][j][k1][k2][k3][k4])ans = 1; } } } } } cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1243> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ map<int,int> mp; vector<vector<int>> nextS(9); void init(){ int masu[4][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, }; int idx = 0; for(int i = 0; i < 3;i++){ for(int j = 0; j < 3;j++){ int S = 0; S = (1<<masu[i][j]) + (1<<masu[i][j+1]) + (1<<masu[i+1][j]) + (1<<masu[i+1][j+1]); mp[S] = idx++; //cout << S << " "; } //cout << endl; } /* 51 102 204 816 1632 3264 13056 26112 52224 */ nextS[0] = vector<int>{51,102,204,816,13056}; nextS[1] = vector<int>{51,102,204,1632,26112}; nextS[2] = vector<int>{51,102,204,3264,52224}; nextS[3] = vector<int>{51,816,1632,3264,13056}; nextS[4] = vector<int>{102,816,1632,3264,26112}; nextS[5] = vector<int>{204,816,1632,3264,52224}; nextS[6] = vector<int>{51,816,13056,26112,52224}; nextS[7] = vector<int>{102,1632,13056,26112,52224}; nextS[8] = vector<int>{204,3264,13056,26112,52224}; } set<tuple<int,int,ll>> memo; bool dfs(int n,int S,ll clowdcnt,const vector<int>& state){ if(n == state.size()) return true; if((n < state.size()) && (S & state[n])) return false; if(memo.find(tuple<int,int,ll>(n,S,clowdcnt))!=memo.end()) return false; memo.insert(tuple<int,int,ll>(n,S,clowdcnt)); for(int i = 0; i < 16; i++){ ll nonrain = (clowdcnt>>(i*3))&7; clowdcnt ^= nonrain<<(i*3); nonrain++; if((S>>i)&1) nonrain = 0; if(nonrain == 7) return false; clowdcnt |= (nonrain<<(i*3)); } int idx = mp[S]; for(auto next:nextS[idx]){ if(dfs(n+1,next,clowdcnt,state))return true; } return false; } bool solve(int N){ memo.clear(); vector<int> state(N); for(int i = 0; i < N;i++){ int B = 0; for(int j = 0; j < 16;j++){ int c; cin >> c; B += (c<<j); } state[i] = B; // cout << bitset<16>(state[i]) << endl; } int beginS = (1<<5) + (1<<6) + (1<<9) + (1<<10); return dfs(0,beginS,0,state); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); int N; while(cin >> N,N){ cout << solve(N) << endl; } return 0; }
#include <stdio.h> #include <cmath> #include <algorithm> #include <cfloat> #include <stack> #include <queue> #include <vector> #include <string.h> #include <set> #include <bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; int diff_row_sun[4] = {0,0,1,1},diff_col_sun[4] = {0,1,0,1}; int diff_row[9] = {-2,-1,0,0,0,0,0,1,2},diff_col[9] = {0,0,-2,-1,0,1,2,0,0}; int N; struct Info{ int row,col; //?????????????????§?¨? int rain_table[16],day; }; bool rangeCheck(int row,int col){ if(row >= 0 && row <= 2 && col >= 0 && col <= 2){ return true; }else{ return false; } } std::set<tuple<int,int,int,ull>> SET; //day,row,col,hash decltype(SET)::iterator IT; void func(){ SET.clear(); int table[N+1][16]; for(int i = 1; i <= N; i++){ for(int k = 0; k < 16; k++)scanf("%d",&table[i][k]); } Info first; first.row = 1; first.col = 1; for(int i = 0; i < 16; i++)first.rain_table[i] = 0; first.day = 1; bool FLG = false; bool check; //?????\????¢?????????¨???????????´??????1????????£???????????? if(table[1][5] == 1 || table[1][6] == 1 || table[1][9] == 1 || table[1][10] == 1){ printf("0\n"); return; } queue<Info> Q; Q.push(first); int weather_map[4][4],row,col,tmp_row,tmp_col,next_row,next_col; while(!Q.empty()){ //??¨????????£??????????????\??°????????????????????´??° for(int i = 0; i < 16; i++){ Q.front().rain_table[i]++; } //??¨????????£?????\???0????????? Q.front().rain_table[4*Q.front().row+Q.front().col] = 0; Q.front().rain_table[4*Q.front().row+Q.front().col+1] = 0; Q.front().rain_table[4*(Q.front().row+1)+Q.front().col] = 0; Q.front().rain_table[4*(Q.front().row+1)+Q.front().col+1] = 0; //7??\??\?????¨?????????????????°???????????£???????????? check = true; for(int i = 0; i < 16; i++){ if(Q.front().rain_table[i] >= 7){ check = false; break; } } if(!check){ Q.pop(); continue; } //????????¢?????? if(Q.front().day >= N){ FLG = true; break; } //?§?????????? for(int a = 0; a < 9; a++){ next_row = Q.front().row + diff_row[a]; next_col = Q.front().col + diff_col[a]; //?§????????????¶???:?????????????????????????????????????????? //day??\??????????????¨???????????¶?????????????????? row = 0,col = 0; for(int i = 0; i < 16; i++){ weather_map[row][col++] = table[Q.front().day+1][i]; if(col >= 4){ col = 0; row++; } } //?§??????????????????????????????????1????????? if(rangeCheck(next_row,next_col)){ check = true; for(int k = 0; k < 4; k++){ tmp_row = next_row + diff_row_sun[k]; tmp_col = next_col + diff_col_sun[k]; if(weather_map[tmp_row][tmp_col] == 1){ check = false; break; } } if(check){ //???????????\???????¨???? ull pre = 0; ull current = 0; for(int i = 0; i < 16; i++){ current = Q.front().rain_table[i] + pre*MOD; pre = current; } auto Data = make_tuple(Q.front().day+1,next_row,next_col,pre); if(SET.count(Data) != 0){ check = false; }else{ SET.insert(Data); } } if(check){ Info next; next.row = next_row; next.col = next_col; for(int k = 0; k < 16; k++)next.rain_table[k] = Q.front().rain_table[k]; next.day = Q.front().day+1; Q.push(next); } } } Q.pop(); } if(!FLG){ printf("0\n"); }else{ printf("1\n"); } } int main(){ while(true){ scanf("%d",&N); if(N == 0)break; func(); } return 0; }
// template {{{ #include <bits/stdc++.h> using namespace std; #define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) loop(i, 0, n) #define rloop(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--) #define rrep(i, n) rloop(i, 0, n) #define eb emplace_back #define ef emplace_front #define pb pop_back #define pf pop_front #define all(c) std::begin(c), std::end(c) #define mp std::make_pair #define mt std::make_tuple #define fi first #define se second #define popcnt __builtin_popcountll using uint = unsigned; using ll = long long; using ull = unsigned long long; using ld = long double; template<typename T> using max_pq = priority_queue<T, vector<T>, less<T>>; template<typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>; const int MOD = 1e9 + 7; const int INF = 1e9 + 10; const ll LLINF = 1e18 + 10; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, -1, 0, 1}; const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template<typename T> inline T sq(T x){ return x * x; } template<typename T, typename U> inline bool chmax(T &x, U y){ if (x >= y) return false; x = y; return true; } template<typename T, typename U> inline bool chmin(T &x, U y){ if (x <= y) return false; x = y; return true; } template<typename T> inline void sort(T &c){ std::sort(std::begin(c), std::end(c)); } template<typename T> inline void reverse(T &c){ std::reverse(std::begin(c), std::end(c)); } template<typename T> inline void unique(T &c){ std::sort(std::begin(c), std::end(c)); c.erase(std::unique(all(c)), std::end(c)); } // }}} #define R (rand() < RAND_MAX / 2) const int T1[] = { 51, 102, 204, 816, 1632, 3264, 13056, 26112, 52224 }; const int T2[][5] = { {0, 1, 2, 3, 6}, {0, 1, 2, 4, 7}, {0, 1, 2, 5, 8}, {0, 3, 4, 5, 6}, {1, 3, 4, 5, 7}, {2, 3, 4, 5, 8}, {0, 3, 6, 7, 8}, {1, 4, 6, 7, 8}, {2, 5, 6, 7, 8} }; const int M1 = 9; const int M7 = 4782969; int n; int s[366]; set<int> vis[366][9]; int dfs(int d, int p, int st) { if (vis[d][p].count(st)) return 0; if (d >= 7){ int x = st, y = 0; rep(i, 7){ y |= T1[x % M1]; x /= 9; } if (y != (1 << 16) - 1){ // vis[d][p].insert(st); return 0; } } if (d == n) return 1; if (s[d] & T1[p]){ if (R) vis[d][p].insert(st); return 0; } for (int to : T2[p]){ if (dfs(d + 1, to, (st * 9 + p) % M7)){ return 1; } } if (R) vis[d][p].insert(st); return 0; } int main() { while (cin >> n, n){ rep(i, 366) rep(j, 9) vis[i][j].clear(); rep(i, n){ s[i] = 0; rrep(j, 16){ int b; cin >> b; s[i] = s[i] * 2 + b; } } cout << dfs(0, 4, 0) << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; int N, a[512]; using P = tuple<int, int, ll>; map<P, int> done; bool contain(int x, int s) { return (x & s) == x; } bool dfs(int cur, int lev, ll cnt) { if (lev < N && (cur & a[lev])) return 0; P p = P(cur, lev, cnt); if (done.count(p)) return 0; done[p]++; for (int i = 0; i < 16; i++) { ll c = (cnt >> (i*3)) & 7; cnt ^= c << (i*3); if (cur >> i & 1) c = 0; else c++; if (c == 7) return 0; cnt ^= c << (i*3); } if (lev == N) return 1; if (dfs(cur, lev+1, cnt)) return 1; if (contain(cur, 61166) && dfs(cur >> 1, lev+1, cnt)) return 1; if (contain(cur, 52428) && dfs(cur >> 2, lev+1, cnt)) return 1; if (contain(cur, 30583) && dfs(cur << 1, lev+1, cnt)) return 1; if (contain(cur, 13107) && dfs(cur << 2, lev+1, cnt)) return 1; if (contain(cur, 65520) && dfs(cur >> 4, lev+1, cnt)) return 1; if (contain(cur, 65280) && dfs(cur >> 8, lev+1, cnt)) return 1; if (contain(cur, 4095) && dfs(cur << 4, lev+1, cnt)) return 1; if (contain(cur, 255) && dfs(cur << 8, lev+1, cnt)) return 1; return 0; } void solve() { done.clear(); cout << dfs(1632, 0, 0) << endl; } bool input() { cin >> N; if (N == 0) return 0; for (int i = 0; i < N; i++) { a[i] = 0; for (int j = 0; j < 16; j++) { int tmp; cin >> tmp; a[i] |= tmp << j; } } return 1; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); while (input()) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<x<<endl #define mmax(x,y) (x>y?x:y) #define mmin(x,y) (x<y?x:y) #define maxch(x,y) x=mmax(x,y) #define minch(x,y) x=mmin(x,y) #define uni(x) x.erase(unique(all(x)),x.end()) #define exist(x,y) (find(all(x),y)!=x.end()) #define bcnt __builtin_popcount #define INF 1e16 #define mod 1000000007 int n; bool dp[367][9][8][8][8][8]; int main(){ while(1){ cin>>n; if(n==0)break; vector<int> pos; rep(q,n){ vector<vector<int> > a(4,vector<int>(4)); rep(i,4)rep(j,4){ cin>>a[i][j]; } pos.push_back(0); rep(i,3)rep(j,3){ if(a[i][j]+a[i][j+1]+a[i+1][j]+a[i+1][j+1]!=0){ pos.back()|=1<<(i*3+j); } } } if((pos[0]>>4)&1){ cout<<0<<endl; continue; } memset(dp,0,sizeof(dp)); dp[1][4][1][1][1][1]=true; bool res=false; repl(i,1,n){ rep(p,9){ rep(a,7)rep(b,7)rep(c,7)rep(d,7){ if(!dp[i][p][a][b][c][d])continue; rep(np,9){ if(((pos[i]>>np)&1)||(p%3!=np%3&&p/3!=np/3))continue; if(np==0){ dp[i+1][np][0][b+1][c+1][d+1]=true; }else if(np==2){ dp[i+1][np][a+1][0][c+1][d+1]=true; }else if(np==6){ dp[i+1][np][a+1][b+1][0][d+1]=true; }else if(np==8){ dp[i+1][np][a+1][b+1][c+1][0]=true; }else{ dp[i+1][np][a+1][b+1][c+1][d+1]=true; } } } } } rep(p,9)rep(a,7)rep(b,7)rep(c,7)rep(d,7)if(dp[n][p][a][b][c][d])res=true; cout<<(res?1:0)<<endl; } return 0; }
#include<iostream> #include<queue> #include<bitset> #include<cassert> #include<vector> #include<algorithm> #include<climits> #include<cstdio> #define REP(i,s,n) for(int i=s;i<n;i++) #define rep(i,n) REP(i,0,n) #define IINF (INT_MAX) #define MAX 366 using namespace std; int dx[] = {+0,+1,+0,-1,+0,+2,+0,-2,+0}; int dy[] = {+1,+0,-1,+0,+2,+0,-2,+0,+0}; int n; int fest[MAX]; bool visited[MAX][9][7][7][7][7]; bool isValid(int day,int cur) { if((fest[day]>>cur) & 1)return false; if((fest[day]>>(cur+1)) & 1)return false; if((fest[day]>>(cur+4)) & 1)return false; if((fest[day]>>(cur+5)) & 1)return false; return true; } bool dfs(int day,int cur,int _1,int _3,int _9,int _11) { //cout << day << "," << cur << "," << _1 << "," << _3 << "," << _9 << "," << _11 << endl; if(visited[day][cur-(1*(cur/4))][_1][_3][_9][_11])return false; visited[day][cur-(1*(cur/4))][_1][_3][_9][_11] = true; if(!isValid(day,cur)) { return false; } if(day >= n-1) { return true; } int x = cur % 4; int y = cur / 4; rep(i,9) { int nx = x + dx[i]; int ny = y + dy[i]; if(!(0 <= nx && nx <= 2 && 0 <= ny && ny <= 2))continue; int next = nx + ny * 4; int cost1 = _1+1; int cost3 = _3+1; int cost9 = _9+1; int cost11 = _11+1; if(next == 0)cost1 = 0; if(next == 2)cost3 = 0; if(next == 8)cost9 = 0; if(next == 10)cost11 = 0; if(cost1 >= 7 || cost3 >= 7 || cost9 >= 7 || cost11 >= 7)continue; //cout << "go next (" << day+1 << "," << next << "," << cost1 << "," <<cost3 << "," << cost9 << "," << cost11 << ")\n"; if(dfs(day+1,next,cost1,cost3,cost9,cost11)) { return true; } } return false; } int main() { while(scanf("%d",&n),n) { rep(i,n) { fest[i] = 0; int schedule; rep(j,16) { scanf("%d",&schedule); if(schedule) { fest[i] |= (1<<j); } } } rep(i,n+1)rep(j,9)rep(k,7)rep(l,7)rep(m,7)rep(o,7) visited[i][j][k][l][m][o] = false; cout << dfs(0,5,1,1,1,1) << endl; } return 0; }
#include <vector> #include <map> #include <set> #include <stack> #include <queue> #include <algorithm> #include <utility> #include <functional> #include <sstream> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <climits> #include <fstream> #include <list> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;} template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define REP(i,n) FOR(i,0,(n)-1) const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = INT_MAX/10; int dx[] = {1, 0, -1, 0, 2, 0, -2, 0, 0}; int dy[] = {0, 1, 0, -1, 0, 2, 0, -2, 0}; int cx[] = {0, 1, 0, 1}; int cy[] = {0, 0, 1, 1}; struct state { int d, x, y, ul, ur, dl, dr; state(int d, int x, int y, int ul, int ur, int dl, int dr) : d(d), x(x), y(y), ul(ul), ur(ur), dl(dl), dr(dr) {}; }; bool isProperXY(int x, int y) { return 0<=x && x<=2 && 0<=y && y<=2; } bool isProperF(int ul, int ur, int dl, int dr) { return ul<7 && ur<7 && dl<7 && dr<7; } bool visited[366][3][3][7][7][7][7]; int main() { int N; while(cin >> N, N) { vector<vvi> f(N, vvi(4, vi(4))); REP(i, N) { REP(j, 4) { REP(k, 4) { cin >> f[i][j][k]; } } } stack<state> S; memset(visited, false, sizeof(visited)); bool ok = true; REP(c, 4) { if(f[0][1+cy[c]][1+cx[c]]) { ok = false; break; } } if(ok) { S.push(state(1, 1, 1, 1, 1, 1, 1)); visited[1][1][1][1][1][1][1] = true; } bool finished = false; while(!S.empty()) { state st = S.top(); S.pop(); if(st.d == N) { finished = true; break; } int nd = st.d+1; REP(d, 9) { int nx = st.x+dx[d], ny = st.y+dy[d]; if(isProperXY(nx, ny)) { ok = true; REP(c, 4) { if(f[st.d][ny+cy[c]][nx+cx[c]]) { ok = false; break; } } int nul = st.ul+1, nur = st.ur+1, ndl = st.dl+1, ndr = st.dr+1; if(nx == 0) { if(ny == 0) { nul = 0; } else if(ny == 2) { ndl = 0; } } else if(nx == 2) { if(ny == 0) { nur = 0; } else if(ny == 2) { ndr = 0; } } if(ok && isProperF(nul, nur, ndl, ndr)) { if(!visited[nd][nx][ny][nul][nur][ndl][ndr]) { visited[nd][nx][ny][nul][nur][ndl][ndr] = true; S.push(state(nd, nx, ny, nul, nur, ndl, ndr)); } } } } } cout << (finished ? 1 : 0 ) << endl; } return 0; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <cassert> #include <string> #include <memory.h> #include <queue> #include <cstdio> #include <cstdlib> #include <set> #include <map> #include <cctype> #include <iomanip> #include <sstream> #include <cctype> #include <fstream> #include <cmath> using namespace std; #define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++) #define REP(i, n) REP2(i, 0, n) #define ALL(c) (c).begin(), (c).end() #define ITER(c) __typeof((c).begin()) #define PB(e) push_back(e) #define FOREACH(i, c) for(ITER(c) i = (c).begin(); i != (c).end(); ++i) #define MP(a, b) make_pair(a, b) #define PARITY(n) ((n) & 1) typedef long long ll; typedef pair<ll, ll> P; const int INF = 1000 * 1000 * 1000 + 7; const double EPS = 1e-10; const int DMAX = 365; bool dp[DMAX + 1][3][3][7][7][7][7]; bool can_move[DMAX + 1][3][3]; int main(){ int D; const int N = 4; memset(dp, false, sizeof(dp)); while(cin >> D && D){ REP(i, D){ int F[4][4]; REP(r, N)REP(c, N) cin >> F[r][c]; REP(r, N - 1)REP(c, N - 1){ can_move[i][r][c] = true; REP(dr, 2)REP(dc, 2) can_move[i][r][c] &= F[r+dr][c+dc] == 0; } } memset(dp, false, sizeof(dp)); dp[0][1][1][0][0][0][0] = true; REP(i, D)REP(r, N - 1)REP(c, N - 1){ if(i == 1 && (r != 1 || c != 1)) continue; int dr[9] = {0, 0, 0, 0, 0, 1, 2, -1, -2}; int dc[9] = {0, 1, 2, -1, -2, 0, 0, 0, 0}; REP(s, 7)REP(t, 7)REP(u, 7)REP(v, 7)if(dp[i][r][c][s][t][u][v]){ REP(p, 9){ int r2 = r + dr[p]; int c2 = c + dc[p]; if(0 <= r2 && r2 < N-1 && 0 <= c2 && c2 < N-1 && can_move[i][r2][c2]){ int s2 = (r2 == 0 && c2 == 0) ? 0 : s + 1; int t2 = (r2 == 0 && c2 == N - 2) ? 0 : t + 1; int u2 = (r2 == N - 2 && c2 == 0) ? 0 : u + 1; int v2 = (r2 == N - 2 && c2 == N - 2) ? 0 : v + 1; if(s2 < 7 && t2 < 7 && u2 < 7 && v2 < 7){ dp[i+1][r2][c2][s2][t2][u2][v2] = true; } } } } } bool ok = false; REP(r, 3)REP(c, 3)REP(s, 7)REP(t, 7)REP(u, 7)REP(v, 7){ if(D > 1){ ok |= dp[D][r][c][s][t][u][v]; }else{ ok |= dp[D][1][1][s][t][u][v]; } } cout << ok << endl; } return 0; }
#include <bits/stdc++.h> #define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++) using namespace std; typedef long long ll; typedef pair< ll, vector<ll> > state; int main(void) { ll N; while(cin >> N, N) { vector<ll> schedule(N, 0); REP(i, 0, N) REP(j, 0, 16) { ll d; cin >> d; schedule[i] = schedule[i] | (d << (15 - j)); } set<state> st; st.insert(state(5, vector<ll>({ 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff }))); ll ans = 0; REP(i, 0, N) { set<state> nst; for(state s : st) { ll p = s.first; vector<ll> past = s.second; ll rain[16] = { 0x3300, 0x6600, 0xcc00, 0x0000, 0x0330, 0x0660, 0x0cc0, 0x0000, 0x0033, 0x0066, 0x00cc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; ll r = rain[p]; ll u = schedule[i] & r; ll t = r; REP(j, 0, 6) t = t | past[j]; if(u == 0 && t == 0xffff) { if(i + 1 == N) ans = 1; vector<ll> current(6); current[0] = r; REP(j, 0, 5) current[j + 1] = past[j]; ll y = p / 4, x = p % 4; REP(dy, -2, 3) REP(dx, -2, 3) if(dy == 0 || dx == 0) { ll ny = y + dy, nx = x + dx; if(0 <= ny && ny < 3 && 0 <= nx && nx < 3) { ll np = ny * 4 + nx; nst.insert(state(np, current)); } } } } swap(st, nst); } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define dump(n) cout<<"# "<<#n<<'='<<(n)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<string> vs; const int INF=1e9; const int MOD=1e9+7; const double EPS=1e-9; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& a){ os<<'['; rep(i,a.size()) os<<(i?" ":"")<<a[i]; return os<<']'; } // (i,j)を左上とする2x2領域で祭りが開かれている bool conflict(int c,int i,int j) { return c&0x33<<i*4+j; } // 7日間雨が降らなかった地域がある bool left7days(ull b) { return b+0x1111111111111111ull&0x8888888888888888ull; } // (i,j)を左上とする2x2領域に雨を降らせた日の翌日 ull next(ull b,int i,int j) { return b+0x1111111111111111ull&~(0xff00ffull<<(i*4+j)*4); } namespace std{ template<> struct hash<tuple<int,int,ull>>{ size_t operator()(const tuple<int,int,ull>& t)const{ const char* ptr=(const char*)(&t); size_t res=2166136261; rep(i,sizeof(t)) (res^=*ptr++)*=16777619; return res; } }; } int main() { for(int n;cin>>n && n;){ vector<int> cs(n); rep(i,n) rep(j,16){ int x; cin>>x; cs[i]|=x<<j; } queue<tuple<int,int,ull>> q; q.emplace(1,1,0); rep(d,n){ decltype(q) q2; unordered_set<tuple<int,int,ull>> vis; while(q.size()){ auto cur=q.front(); q.pop(); int i,j; ull b; tie(i,j,b)=cur; if(i<0 || 3<=i || j<0 || 3<=j) continue; if(conflict(cs[d],i,j)|| left7days(b)) continue; if(vis.count(cur)) continue; vis.insert(cur); rep(k,4) rep(l,3){ int ni=i+"\xff\x1\0\0"[k]*l,nj=j+"\0\0\xff\x1"[k]*l; q2.emplace(ni,nj,next(b,i,j)); } } swap(q,q2); } bool res=false; while(q.size()){ res|=!left7days(get<2>(q.front())); q.pop(); } cout<<res<<endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef tuple<int, int, int, ll> T; int N; int fst[400]; int dx[9] = {0, 0, 1, 0, -1, 0, 2, 0, -2}; int dy[9] = {0, -1, 0, 1, 0, -2, 0, 2, 0}; set<T> memo; bool rain(int px, int py, int idx){ int cloud = px + py * 4; return cloud == idx || cloud + 1 == idx || cloud + 4 == idx || cloud + 5 == idx; } bool dfs(int day, int px, int py, ll state){ //printf("Day %d (%d %d)\n", day, px, py); if(day >= N) return true; if(memo.count(T(day, px, py, state))) return false; memo.insert(T(day, px, py, state)); int rainy = 0; for(int y = 0; y < 4; y++){ for(int x = 0; x < 4; x++){ ll idx = x + y * 4; ll cnt = (state >> (idx * 3LL)) & 7LL; if(rain(px, py, idx)) { cnt = 0; rainy |= (1 << idx); }else{ cnt++; } if(cnt >= 7) return false; state = (state & ~(7LL << (idx * 3LL))) | (cnt << (idx * 3LL)); } } if((fst[day] & rainy)) return false; for(int i = 0; i < 9; i++){ int nx = px + dx[i]; int ny = py + dy[i]; if(0 <= nx && nx <= 2 && 0 <= ny && ny <= 2){ if(dfs(day + 1, nx, ny, state)) return true; } } return false; } int main(){ while(cin >> N, N){ for(int i = 0; i < N; i++){ fst[i] = 0; for(int j = 0; j < 16; j++){ int v; cin >> v; fst[i] |= (v << j); } } memo.clear(); if(dfs(0, 1, 1, 0)){ cout << 1 << endl; }else{ cout << 0 << endl; } } }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <sstream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <numeric> #include <cctype> #include <tuple> #include <array> #include <cassert> // BEGIN CUT HERE #ifdef _MSC_VER #include <agents.h> #endif // END CUT HERE #define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i) #define rep(i, n) FOR(i, 0, n) #define ALL(v) v.begin(), v.end() #define REV(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()) #define MEMSET(v, s) memset(v, s, sizeof(v)) #define MP make_pair #define MT make_tuple using namespace std; typedef long long ll; typedef pair<int, int> P; typedef unsigned long long ull; int vis[400][7][7][7][7][3][3]; int ng[400][3][3]; int dx[] = { 0, 1, 0, -1 }; int dy[] = { -1, 0, 1, 0 }; int dx2[] = { 0, 1, 0, 1 }; int dy2[] = { 0, 0, 1, 1 }; int n; void dfs(int d, int lt, int rt, int lb, int rb, int x, int y){ if (lt > 6 || rt > 6 || lb > 6 || rb > 6) return; if (d == n){ //cout << lt << ' ' << rt << ' ' << lb << ' ' << rb << endl; throw 1; return; } if (ng[d][x][y] || vis[d][lt][rt][lb][rb][x][y]) return; vis[d][lt][rt][lb][rb][x][y] = 1; ++lt, ++rt, ++lb, ++rb; int pos = y * 3 + x; if (pos == 0) lt = 0; if (pos == 2) rt = 0; if (pos == 6) lb = 0; if (pos == 8) rb = 0; FOR(k, 1, 3) rep(dir, 4){ int nx = x + dx[dir] * k; int ny = y + dy[dir] * k; if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; dfs(d + 1, lt, rt, lb, rb, nx, ny); } dfs(d + 1, lt, rt, lb, rb, x, y); } int main(){ while (cin >> n, n){ MEMSET(vis, 0); MEMSET(ng, 0); rep(d, n) rep(y, 4) rep(x, 4){ int a; cin >> a; if (!a) continue; int xx = x - 1, yy = y - 1; rep(dir, 4){ int nx = xx + dx2[dir], ny = yy + dy2[dir]; if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue; ng[d][nx][ny] = 1; } } try{ dfs(0, 0, 0, 0, 0, 1, 1); cout << 0 << endl; } catch(int x){ cout << 1 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int bit[16]; int memo[366][9][7][7][7][7]; constexpr int idx[9] = {0, 1, 2, 4, 5, 6, 8, 9, 10}; array<array<int, 5>, 9> to = {{{0, 1, 2, 3, 6}, {0, 1, 2, 4, 7}, {0, 1, 2, 5, 8}, {0, 3, 4, 5, 6}, {1, 3, 4, 5, 7}, {2, 3, 4, 5, 8}, {0, 3, 6, 7, 8}, {1, 4, 6, 7, 8}, {2, 5, 6, 7, 8}}}; tuple<int, int, int, int> calc(int d, int d1, int d2, int d3, int d4) { if(d == 0) { d1 = 0; d2++; d3++; d4++; } else if(d == 2) { d2 = 0; d1++; d3++; d4++; } else if(d == 6) { d3 = 0; d1++; d2++; d4++; } else if(d == 8) { d4 = 0; d1++; d2++; d3++; } else { d1++; d2++; d3++; d4++; } return make_tuple(d1, d2, d3, d4); } bool solve(int d, int now, int d1, int d2, int d3, int d4, vector<int> const& v) { int& r = memo[d][now][d1][d2][d3][d4]; if(r != -1) { return r; } if(v[d] & bit[idx[now]]) { return r = false; } if(d == v.size()-1) { return r = true; } bool f = false; for(int i=0; i<5; ++i) { int to2 = to[now][i]; auto t = calc(to2, d1, d2, d3, d4); if(get<0>(t) == 7 || get<1>(t) == 7 || get<2>(t) == 7 || get<3>(t) == 7) { continue; } f |= solve(d+1, to2, get<0>(t), get<1>(t), get<2>(t), get<3>(t), v); } return r = f; } int main() { int init = 0b1100110000000000; for(int i=0; i<16; ++i) { bit[i] = init; init >>= 1; } int D; while(cin >> D, D) { vector<int> v(D); for(int i=0; i<D; ++i) { int bit = 0; for(int j=0; j<16; ++j) { bit <<= 1; int b; cin >> b; bit += b; } v[i] = bit; } for(int i=0; i<366; ++i) { for(int j=0; j<9; ++j) { for(int k=0; k<7; ++k) { for(int l=0; l<7; ++l) { for(int m=0; m<7; ++m) { for(int n=0; n<7; ++n) { memo[i][j][k][l][m][n] = -1; } } } } } } //fill(memo[0][0][0][0][0], memo[366][9][7][7][7], -1); cout << solve(0, 4, 1, 1, 1, 1, v) << endl; } }
#include <bits/stdc++.h> #define N 366 using namespace std; int n,A[N][4][4]; int mem[N][3][3][7][7][7][7]; int dx[]={0,0,0,1,-1}; int dy[]={0,1,-1,0,0}; int check(int d,int x,int y){ if(x<0||y<0||x>=3||y>=3)return 0; for(int i=0;i<2;i++) for(int j=0;j<2;j++)if(A[d][y+i][x+j]) return 0; return 1; } void getABCD(int x,int y,int &A,int &B,int &C,int &D){ A++,B++,C++,D++; if(x==0&&y==0) A = 0; if(x==2&&y==0) B = 0; if(x==0&&y==2) C = 0; if(x==2&&y==2) D = 0; } int dfs(int num=0,int x=1,int y=1,int A=1,int B=1,int C=1,int D=1){ if(num==n-1) return 1; if(mem[num][x][y][A][B][C][D]++) return 0; for(int i=0;i<5;i++) for(int j=0,nx=x,ny=y;j<2;j++){ nx+=dx[i],ny+=dy[i]; int nA=A,nB=B,nC=C,nD=D; getABCD(nx,ny,nA,nB,nC,nD); if(nA==7||nB==7||nC==7||nD==7)continue; if(check(num+1,nx,ny)&&dfs(num+1,nx,ny,nA,nB,nC,nD))return 1; } return 0; } int main(){ while(cin>>n,n){ for(int i=0;i<n;i++) for(int j=0;j<16;j++) cin>>A[i][j/4][j%4]; memset(mem,0,sizeof(mem)); cout<<check(0,1,1)*dfs()<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int MAX_N = 365; constexpr int SIZE = 16; constexpr int rain[3][3] = {{0x0033, 0x0066, 0x00cc}, {0x0330, 0x0660, 0x0cc0}, {0x3300, 0x6600, 0xcc00}}; constexpr int dx[9] = {0, 1, -1, 2, -2, 0, 0, 0, 0}; constexpr int dy[9] = {0, 0, 0, 0, 0, 1, -1, 2, -2}; int n; int festivals[MAX_N]; unordered_map<int, int> get_index; unordered_set<int> visited[MAX_N][3][3]; inline int calc_value(int idx, const array<int, 7> &prev) { int res = 0; for(int i = 0; i < 7; ++i) { if(idx != i) res = res * 9 + get_index[prev[i]]; } return res; } bool dfs(int day, int x, int y, array<int, 7> &prev) { if(day == n) return true; const int idx = day % 7; const int value = calc_value(idx, prev); if(visited[day][x][y].count(value)) return false; int need = (1 << SIZE) - 1; for(int i = 0; i < 7; ++i) { if(idx != i) need &= ~prev[i]; } for(int d = 0; d < 9; ++d) { const int nx = x + dx[d]; const int ny = y + dy[d]; if(nx < 0 || ny < 0 || nx >= 3 || ny >= 3) continue; if(need & ~rain[ny][nx]) continue; if(festivals[day] & rain[ny][nx]) continue; const int tmp = prev[idx]; prev[idx] = rain[ny][nx]; if(dfs(day + 1, nx, ny, prev)) return true; prev[idx] = tmp; } visited[day][x][y].insert(value); return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for(int x = 0; x < 3; ++x) { for(int y = 0; y < 3; ++y) { get_index[rain[y][x]] = y * 3 + x; } } while(cin >> n && n) { memset(festivals, 0, sizeof(festivals)); for(int i = 0; i < n; ++i) { for(int j = 0; j < SIZE; ++j) { int in; cin >> in; festivals[i] |= (in << j); } } if(festivals[0] & rain[1][1]) { cout << 0 << endl; continue; } array<int, 7> prev; prev.fill((1 << SIZE) - 1); prev[0] = rain[1][1]; for(int i = 0; i < n; ++i) { for(int x = 0; x < 3; ++x) { for(int y = 0; y < 3; ++y) { visited[i][x][y].clear(); } } } cout << dfs(1, 1, 1, prev) << endl; } return EXIT_SUCCESS; }
#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 = 1000000007; int dx[9] = {1, 0, -1, 0, 0, 0, 2, -2, 0}; int dy[9] = {0, 1, 0, -1, 0, 2, 0, 0, -2}; int main(){ int D; int sch[365][4][4] = {}; while(cin>>D && D){ bool goal = false; REP(i, D) REP(y, 4) REP(x, 4) cin>>sch[i][y][x]; static bool used[366][16][2401]; memset(used, 0, sizeof(used)); queue<int> qd, qy, qx; queue< vector<int> > qv; vector<int> s(4, 0); qd.push(0); qx.push(1); qy.push(1); qv.push(s); while(!qd.empty()){ int d = qd.front(); qd.pop(); int x = qx.front(); qx.pop(); int y = qy.front(); qy.pop(); //printf("d = %d x = %d y = %d\n", d, x, y); vector<int> v = qv.front(); qv.pop(); if(d == D){ goal = true; break; } REP(i, 4) v[i]++; bool ok = true; REP(dy, 2)REP(dx, 2) if(sch[d][y + dy][x + dx] == 1) ok = false; if(x == 0 && y == 0) v[0] = 0; if(x == 2 && y == 0) v[1] = 0; if(x == 0 && y == 2) v[2] = 0; if(x == 2 && y == 2) v[3] = 0; REP(i, 4) if(v[i] >= 7) ok = false; if(!ok) continue; REP(r, 9){ bool ok2 = true; int nx = x + dx[r]; int ny = y + dy[r]; REP(dx, 2)REP(dy, 2) ok2 &= valid(nx + dx, ny + dy, 4, 4); if(!ok2) continue; if(used[d + 1][ny + nx * 4][v[0] + 7 * v[1] + 49 * v[2] + 343 * v[3]]) continue; used[d + 1][ny + nx * 4][v[0] + 7 * v[1] + 49 * v[2] + 343 * v[3]] = true; qd.push(d + 1); qx.push(nx); qy.push(ny); qv.push(v); } } if(goal) cout<<1<<endl; else cout<<0<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<b;i++) int n; int t[365][4][4]; map<int, int> memo; int dfs(int day, int x, int y, int lu, int ru, int lb, int rb) { int id = day; id = id * 10 + x; id = id * 10 + y; id = id * 10 + lu; id = id * 10 + ru; id = id * 10 + lb; id = id * 10 + rb; if (memo.find(id) != memo.end()) return memo[id]; if(day == n) return (memo[id] = 1); rep(i, 0, 2) rep(j, 0, 2) { int xx = x + i; int yy = y + j; if (t[day][yy][xx] == 1) return (memo[id] = 0); } if (lu == 7) return (memo[id] = 0); if (ru == 7) return (memo[id] = 0); if (lb == 7) return (memo[id] = 0); if (rb == 7) return (memo[id] = 0); rep(xx, 0, 3) rep(yy, 0, 3) { if (x != xx && y != yy) continue; int ret; if (xx == 0 && yy == 0) ret = dfs(day + 1, xx, yy, 0, ru + 1, lb + 1, rb + 1); else if(xx == 0 && yy == 2) ret = dfs(day + 1, xx, yy, lu + 1, ru + 1, 0, rb + 1); else if(xx == 2 && yy == 0) ret = dfs(day + 1, xx, yy, lu + 1, 0, lb + 1, rb + 1); else if(xx == 2 && yy == 2) ret = dfs(day + 1, xx, yy, lu + 1, ru + 1, lb + 1, 0); else ret = dfs(day + 1, xx, yy, lu + 1, ru + 1, lb + 1, rb + 1); if(ret) return (memo[id] = 1); } return (memo[id] = 0); } int main() { cin.tie(0); ios::sync_with_stdio(false); while (1) { cin >> n; if (n == 0) return 0; memo.clear(); rep(i, 0, n) rep(j, 0, 4) rep(k, 0, 4) cin >> t[i][j][k]; cout << dfs(0, 1, 1, 1, 1, 1, 1) << endl; } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1243> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ map<int,int> mp; vector<vector<int>> nextS(9); void init(){ int masu[4][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, }; int idx = 0; for(int i = 0; i < 3;i++){ for(int j = 0; j < 3;j++){ int S = 0; S = (1<<masu[i][j]) + (1<<masu[i][j+1]) + (1<<masu[i+1][j]) + (1<<masu[i+1][j+1]); mp[S] = idx++; //cout << S << " "; } //cout << endl; } /* 51 102 204 816 1632 3264 13056 26112 52224 */ nextS[0] = vector<int>{51,102,204,816,13056}; nextS[1] = vector<int>{51,102,204,1632,26112}; nextS[2] = vector<int>{51,102,204,3264,52224}; nextS[3] = vector<int>{51,816,1632,3264,13056}; nextS[4] = vector<int>{102,816,1632,3264,26112}; nextS[5] = vector<int>{204,816,1632,3264,52224}; nextS[6] = vector<int>{51,816,13056,26112,52224}; nextS[7] = vector<int>{102,1632,13056,26112,52224}; nextS[8] = vector<int>{204,3264,13056,26112,52224}; } set<tuple<int,int,ll>> memo; bool dfs(int n,int S,ll clowdcnt,const vector<int>& state){ if(n == state.size()) return true; if(S & state[n]) return false; if(memo.find(tuple<int,int,ll>(n,S,clowdcnt))!=memo.end()) return false; memo.insert(tuple<int,int,ll>(n,S,clowdcnt)); for(int i = 0; i < 16; i++){ ll nonrain = (clowdcnt>>(i*3))&7; clowdcnt ^= nonrain<<(i*3); nonrain++; if((S>>i)&1) nonrain = 0; if(nonrain == 7) return false; clowdcnt |= (nonrain<<(i*3)); } int idx = mp[S]; for(auto next:nextS[idx]) if(dfs(n+1,next,clowdcnt,state))return true; return false; } bool solve(int N){ memo.clear(); vector<int> state(N); for(int i = 0; i < N;i++){ int B = 0; for(int j = 0; j < 16;j++){ int c; cin >> c; B += (c<<j); } state[i] = B; } int beginS = (1<<5) + (1<<6) + (1<<9) + (1<<10); return dfs(0,beginS,0,state); } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); init(); int N; while(cin >> N,N){ cout << solve(N) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector< pair< int, int > > g[100001]; vector< int > leftt[100001], rightt[100001], just[100001]; vector< int > gg[100001]; int deg[100001]; int rec(int idx, int back = -1) { if(rightt[idx][0] == INF) { for(int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if(i == back) continue; just[idx][i] = rec(to, rev) + 1; } for(int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for(int i = (int) g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if(back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector< pair< int, int > > edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for(int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if(t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if(uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for(auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); ++deg[uf.find(e.second)]; } vector< int > order; for(int i = 0; i < N; i++) { if(deg[i] == 0) order.push_back(i); } for(int i = 0; i < order.size(); i++) { for(auto &e : gg[order[i]]) { if(--deg[e] == 0) order.push_back(e); } } if(order.size() != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for(int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for(int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }
#include<bits/stdc++.h> using namespace std; using Int = long long; struct QuickFind{ Int n; vector<Int> r,p; vector<vector<Int> > v; QuickFind(){} QuickFind(Int sz):n(sz),r(sz),p(sz),v(sz){ for(Int i=0;i<n;i++){ r[i]=1,p[i]=i; v[i].resize(1,i); } } bool same(Int x,Int y){ return p[x]==p[y]; } void unite(Int x,Int y){ x=p[x];y=p[y]; if(x==y) return; if(r[x]<r[y]) swap(x,y); r[x]+=r[y]; for(Int i=0;i<(Int)v[y].size();i++){ p[v[y][i]]=x; v[x].push_back(v[y][i]); } v[y].clear(); } Int find(Int v){return p[v];}; }; struct SCC{ Int n; vector<vector<Int> > G,rG,T,C; vector<Int> vs,used,belong; SCC(){} SCC(Int sz):n(sz),G(sz),rG(sz),used(sz),belong(sz){} void add_edge(Int from,Int to){ G[from].push_back(to); rG[to].push_back(from); } void input(Int m,Int offset=0){ Int a,b; for(Int i=0;i<m;i++){ cin>>a>>b; add_edge(a+offset,b+offset); } } void dfs(Int v){ used[v]=1; for(Int i=0;i<(Int)G[v].size();i++){ if(!used[G[v][i]]) dfs(G[v][i]); } vs.push_back(v); } void rdfs(Int v,Int k){ used[v]=1; belong[v]=k; C[k].push_back(v); for(Int i=0;i<(Int)rG[v].size();i++){ if(!used[rG[v][i]]) rdfs(rG[v][i],k); } } Int build(){ fill(used.begin(),used.end(),0); vs.clear(); for(Int v=0;v<n;v++){ if(!used[v]) dfs(v); } fill(used.begin(),used.end(),0); Int k=0; for(Int i=vs.size()-1;i>=0;i--){ if(!used[vs[i]]){ T.push_back(vector<Int>()); C.push_back(vector<Int>()); rdfs(vs[i],k++); } } for(Int i=0;i<n;i++) for(Int u:G[i]) if(belong[i]!=belong[u]) T[belong[i]].push_back(belong[u]); for(Int i=0;i<k;i++){ sort(T[i].begin(),T[i].end()); T[i].erase(unique(T[i].begin(),T[i].end()),T[i].end()); } return k; } }; void inf(){ cout<<"Infinite"<<endl; exit(0); } template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} signed main(){ Int n,m; cin>>n>>m; vector<Int> x(m),y(m),w(m); for(Int i=0;i<m;i++) cin>>x[i]>>y[i]>>w[i]; QuickFind uf(n); vector<vector<Int> > G(n),H(n); for(Int i=0;i<m;i++){ x[i]--;y[i]--; if(w[i]==2){ if(uf.same(x[i],y[i])) inf(); uf.unite(x[i],y[i]); G[x[i]].emplace_back(y[i]); G[y[i]].emplace_back(x[i]); } } vector<Int> v; for(Int i=0;i<n;i++) if(uf.find(i)==i) v.emplace_back(i); Int k=v.size(); map<Int, Int> idx; for(Int i=0;i<k;i++) idx[v[i]]=i; SCC scc(k); for(Int i=0;i<m;i++){ if(w[i]==2) continue; H[x[i]].emplace_back(y[i]); Int s=idx[uf.find(x[i])]; Int t=idx[uf.find(y[i])]; if(s==t) inf(); scc.add_edge(s,t); } Int t=scc.build(); if(t!=k) inf(); vector<Int> dp1(n,0),dp2(n,0); vector<Int> vs; for(Int i=0;i<k;i++) vs.emplace_back(v[scc.C[i][0]]); reverse(vs.begin(),vs.end()); function<Int(int,Int)> dfs=[&](Int v,Int p)->Int{ Int res=dp1[v]; for(Int u:G[v]){ if(u==p) continue; chmax(res,dfs(u,v)+1); } return dp2[v]=res; }; function<void(Int,int,Int)> dfs2=[&](Int v,Int p,Int d)->void{ using P = pair<Int, Int>; vector<P> ds; ds.emplace_back(d,p); ds.emplace_back(dp1[v],v); for(Int u:G[v]){ if(u==p) continue; ds.emplace_back(dp2[u]+1,u); } sort(ds.rbegin(),ds.rend()); for(Int u:G[v]){ if(u==p) continue; dfs2(u,v,ds[u==ds[0].second].first+1); } chmax(dp2[v],d); }; for(Int x:vs){ for(Int v:uf.v[x]) for(Int u:H[v]) chmax(dp1[v],dp2[u]+1); //cout<<x<<endl; dfs(x,-1); dfs2(x,-1,0); } if(0) for(Int i=0;i<n;i++) cout<<i<<":"<<dp1[i]<<" "<<dp2[i]<<endl; Int ans=0; for(Int x:dp2) chmax(ans,x); cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 typedef pair<int,int> P; int n,m; int x[MAX_M],y[MAX_M],w[MAX_M]; vector<int> G[MAX_N]; vector<int> g[MAX_N]; vector<int> group[MAX_N]; int id[MAX_N]; bool visited[MAX_N]; vector<int> vd; int dp[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(to==prev)continue; if(i&&to==G[pos][i-1])return true; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++){ if(visited[i])continue; if( dfs(i,-1,i) )return true; } vector< int > deg(n); vector< vector<int> > graph(n); for(int i=0;i<m;i++){ if(w[i]==1){ int X=id[ x[i] ]; int Y=id[ y[i] ]; if(X==Y)return true; graph[X].push_back(Y); deg[Y]++; } } queue<int> Q; for(int i=0;i<n;i++) if(deg[i]==0&&id[i]==i) Q.push(i); while(!Q.empty()){ int pos=Q.front();Q.pop(); vd.push_back(pos); for(int to:graph[pos]){ deg[to]--; if(deg[to]==0)Q.push(to); } } for(int i=0;i<n;i++) if(deg[i]>0)return true; return false; } int d[MAX_N]; void init(int root){ root=id[root]; for(int pos:group[root]) d[pos]=-1; } void bfs(int root){ init(root); queue<int> Q; Q.push(root); d[root]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int to:G[pos]){ if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } } void update(int root,int dif){ for(int pos:group[root]) for(int to:g[pos]) dp[to]=max(dp[to],d[pos]+dif+1); } int solve(int root){ int res=0; bfs(root); int maxm=-1,maxmid; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); maxm=-1; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); return res; } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==1){ g[x[i]].push_back(y[i]); } if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if( check() ){ printf("Infinite\n"); return 0; } int ans=0; for(int root : vd ){ if(root!=id[root])continue; ans=max(ans,solve(root)); } printf("%d\n",ans); return 0; }
#include<stdio.h> #include<algorithm> #include<vector> #define N_ 1001000 using namespace std; int n, m, Deg[N_], Q[N_], head, tail, D[N_], Res, cnt; int B1[101000], B2[101000]; vector<int>E[N_], G[101000]; void Make_Edge(int a, int b, int c){ E[a].push_back(b*2+c); } int main(){ int i, a, b, c, x, sz, j; scanf("%d%d",&n,&m); cnt = 2*n; for(i=1;i<=m;i++){ scanf("%d%d%d",&a,&b,&c); if(c==1){ Make_Edge(n+a,b,1); } else{ Make_Edge(cnt+1,cnt+4,1); Make_Edge(cnt+3,cnt+2,1); G[a].push_back(cnt+1); G[b].push_back(cnt+3); cnt+=4; } } for(i=1;i<=n;i++){ Make_Edge(i,i+n,0); sz = G[i].size(); if(!sz)continue; B1[i] = cnt+1; for(j=0;j<sz;j++){ cnt++; Make_Edge(cnt,G[i][j],0); if(j)Make_Edge(cnt, cnt-1,0); } B2[i] = cnt+1; for(j=sz-1;j>=0;j--){ cnt++; Make_Edge(cnt,G[i][j],0); if(j!=sz-1)Make_Edge(cnt, cnt-1,0); } for(j=0;j<sz;j++){ Make_Edge(i,G[i][j],0); Make_Edge(G[i][j]+1,n+i,0); if(j)Make_Edge(G[i][j]+1, B1[i]+j-1,0); if(j!=sz-1)Make_Edge(G[i][j]+1, B2[i]+sz-j-2,0); } } for(i=1;i<=cnt;i++){ for(j=0;j<E[i].size();j++){ Deg[E[i][j]/2]++; } } for(i=1;i<=cnt;i++){ if(!Deg[i])Q[++tail] = i; } while(head < tail){ x = Q[++head]; Res = max(Res, D[x]); for(i=0;i<E[x].size();i++){ Deg[E[x][i]/2]--; D[E[x][i]/2] = max(D[E[x][i]/2], D[x] + E[x][i]%2); if(!Deg[E[x][i]/2])Q[++tail] = E[x][i]/2; } } if(tail != cnt){ printf("Infinite\n"); return 0; } printf("%d\n",Res); }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 int n,m,ans; int x[MAX_M],y[MAX_M],w[MAX_M]; vector<int> G[MAX_N]; vector<int> g[MAX_N]; vector<int> group[MAX_N]; int id[MAX_N]; bool visited[MAX_N]; vector<int> vd; int dp[MAX_N]; int d[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(to==prev)continue; if(i&&to==G[pos][i-1])return true; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++) if(!visited[i]&&dfs(i,-1,i))return true; vector<int> deg(n); for(int pos=0;pos<n;pos++){ for(int i=0;i<(int)g[pos].size();i++){ int to=g[pos][i]; deg[ id[to] ]++; } } queue<int> Q; for(int i=0;i<n;i++) if(deg[i]==0&&id[i]==i) Q.push(i); while(!Q.empty()){ int root=Q.front();Q.pop(); vd.push_back(root); for(int i=0;i<(int)group[root].size();i++){ int pos=group[root][i]; for(int j=0;j<(int)g[pos].size();j++){ int to=g[pos][j]; deg[ id[to] ]--; if(deg[ id[to] ]==0)Q.push(id[to]); } } } for(int i=0;i<n;i++) if(deg[i]>0)return true; return false; } int solve(int S){ for(int i=0;i<(int)group[id[S]].size();i++){ int pos=group[id[S]][i]; d[pos]=-1; } queue<int> Q; Q.push(S); d[S]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } int res=S; for(int i=0;i<(int)group[id[S]].size();i++){ int pos=group[id[S]][i]; if(d[pos]+dp[pos]>d[res]+dp[res])res=pos; ans=max(ans,d[pos]+dp[S]); for(int i=0;i<(int)g[pos].size();i++){ int to=g[pos][i]; dp[to]=max(dp[to],d[pos]+dp[S]+1); } } return res; } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==1){ g[x[i]].push_back(y[i]); } if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if(check()){ printf("Infinite\n"); return 0; } for(int i=0;i<(int)vd.size();i++){ int root=vd[i]; if(root==id[root]) solve(solve(solve(root))); } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector< pair< int, int > > g[100001]; vector< int > leftt[100001], rightt[100001], just[100001]; vector< int > gg[100001]; int deg[100001]; int rec(int idx, int back = -1) { if(back != -1) { int tmp = max(leftt[idx][back], rightt[idx][back + 1]); if(tmp != INF) return (tmp); } if(rightt[idx][0] == INF) { for(int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if(i == back || just[idx][i] != INF) continue; just[idx][i] = rec(to, rev) + 1; } for(int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for(int i = (int) g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if(back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector< pair< int, int > > edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for(int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if(t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if(uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for(auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); ++deg[uf.find(e.second)]; } vector< int > order; for(int i = 0; i < N; i++) { if(deg[i] == 0) order.push_back(i); } for(int i = 0; i < order.size(); i++) { for(auto &e : gg[order[i]]) { if(--deg[e] == 0) order.push_back(e); } } if(order.size() != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for(int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for(int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }
#include <bits/stdc++.h> #define rep(i,N) for(int i=0;i<(int)N;i++) #define rep1(i,N) for(int i=1;i<=(int)N;i++) #define pb push_back #define all(c) c.begin(),c.end() #define show(x) cout<<#x<<" "<<x<<endl #define chmax(x,y) x=max(x,y) #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024)); #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_); using namespace std; const int MN=100000; struct edge {int to,w,rev;}; typedef vector< vector<edge> > Graph; Graph G,oG; void add_edge(int x,int y,int w,int way){ if(way==1){ G[x].pb({y,w,-1}); }else{ int X=G[x].size(),Y=G[y].size(); G[x].pb({y,w,Y}); G[y].pb({x,w,X}); } } typedef pair<int,int> P; int memo[300000][4]; bool vis[300000][4]; int inf=1e8; int dfs(int v,int eid){ eid++; if(memo[v][eid]!=-1) return memo[v][eid]; if(vis[v][eid]){ return inf; // puts("Infinite"); } vis[v][eid]=1; int ret=0; // rep(i,G[v].size()) if(i!=eid-1){ // edge& e=G[v][i]; // chmax(ret,dfs(e.to,e.rev)+e.w); // } if(G[v].size()>0 && eid!=1) chmax(ret,dfs(G[v][0].to,G[v][0].rev)+G[v][0].w); if(G[v].size()>1 && eid!=2) chmax(ret,dfs(G[v][1].to,G[v][1].rev)+G[v][1].w); if(G[v].size()>2 && eid!=3) chmax(ret,dfs(G[v][2].to,G[v][2].rev)+G[v][2].w); memo[v][eid]=ret; return ret; } int rs[100001]={}; int its[100000]={}; int main(){ BEGIN_STACK_EXTEND(128*1024*1024); int N,M; cin>>N>>M; oG.resize(N); rep(i,M){ int x,y,w; scanf("%d %d %d",&x,&y,&w); x--,y--; if(w==1){ oG[x].pb(edge{y,1,-1}); }else{ int X=oG[x].size(),Y=oG[y].size(); oG[x].pb(edge{y,1,Y}); oG[y].pb(edge{x,1,X}); } } int newN=0; rep(i,N){ int sz=oG[i].size(); if(sz==0) sz=1; rs[i]=newN; its[i]=rs[i]; newN+=sz; } rs[N]=newN; G.resize(newN); rep(x,N){ int n=rs[x+1]-rs[x]; for(int v=rs[x];v<rs[x]+n-1;v++) add_edge(v,v+1,0,2); for(edge e: oG[x]){ if(e.rev==-1){ //one way add_edge(its[x],rs[e.to],1,1); its[x]++; }else{ if(x<e.to){ add_edge(its[x],its[e.to],1,2); its[x]++; its[e.to]++; } } } } int ans=0; N=newN; // rep(i,N) assert(G[i].size()<=3); rep(i,N) rep(j,4) memo[i][j]=-1; rep(i,N) chmax(ans,dfs(i,-1)); if(ans>=inf) puts("Infinite"); else cout<<ans<<endl; END_STACK_EXTEND; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; typedef long long int64; typedef pair<int,int> PII; int n,m; vector<PII> biEdge[123456]; vector<int> uniEdge[123456]; vector<int> nodes[123456]; struct Edge{ int t,w; Edge(int t,int w):t(t),w(w){} }; vector<Edge> edge[2345678]; //a vertex can reach all // <- <- <- nadj // -> -> -> nadj void addE(int u,int v,int w){ edge[u].push_back(Edge(v,w)); } int findNode(int u,int id,int tp){ int pos = lower_bound(biEdge[u].begin(),biEdge[u].end(),make_pair(id,-1)) - biEdge[u].begin(); return nodes[u][2 + biEdge[u].size() * tp + pos]; } int indeg[2345678]; int main(){ cin>>n>>m; rep(it,m){ int u,v,w; scanf("%d%d%d",&u,&v,&w); --u,--v; if(w==2){ biEdge[u].push_back(make_pair(it,v)); biEdge[v].push_back(make_pair(it,u)); } else { uniEdge[u].push_back(v); } } int V = 0; rep(i,n){ nodes[i].push_back(V++); //in:0 nodes[i].push_back(V++); //out:1 sort(biEdge[i].begin(),biEdge[i].end()); rep(it,biEdge[i].size()){ nodes[i].push_back(V++); nodes[i].push_back(V++); nodes[i].push_back(V++); } int m = biEdge[i].size(); rep(it,biEdge[i].size()){ int lt = it + 2; int rt = m + it + 2; int ct = m * 2 + it + 2; if(it>0) addE(nodes[i][lt],nodes[i][lt-1],0); if(it+1<m) addE(nodes[i][rt],nodes[i][rt+1],0); } } //build edge rep(i,n){ rep(it,uniEdge[i].size()){ int v = uniEdge[i][it];//i->v addE(nodes[i][1],nodes[v][0],1); } int m = biEdge[i].size(); if(m > 0){ rep(it,biEdge[i].size()){ int lt = it + 2; int rt = m + it + 2; int ct = m * 2 + it + 2; if(it > 0){ int id = biEdge[i][it-1].first; int v = biEdge[i][it-1].second; addE(nodes[i][lt],findNode(v,id,2),1); } if(it + 1 < m){ int id = biEdge[i][it+1].first; int v = biEdge[i][it+1].second; addE(nodes[i][rt],findNode(v,id,2),1); } addE(nodes[i][ct],nodes[i][lt],0); addE(nodes[i][ct],nodes[i][rt],0); addE(nodes[i][ct],nodes[i][1],0); } addE(nodes[i][0],nodes[i][2+m],0); { int id = biEdge[i][0].first; int v = biEdge[i][0].second; addE(nodes[i][0],findNode(v,id,2),1); } } addE(nodes[i][0],nodes[i][1],0); } //graph built rep(i,V){ rep(j,edge[i].size()) indeg[edge[i][j].t]++; } static int que[2345678],qh=0,qt=0; rep(i,V) if(indeg[i]==0) que[qt++]=i; while(qh<qt){ int u = que[qh++]; rep(j,edge[u].size()){ int v = edge[u][j].t; indeg[v]--; if(!indeg[v]) que[qt++] = v; } } static int dp[2345678]; if(qt!=V){ puts("Infinite"); } else { rep(i,V){ int u = que[V-1-i]; dp[u]=0; rep(j,edge[u].size()){ int v = edge[u][j].t; dp[u] = max(dp[u],dp[v] + edge[u][j].w); } } int ans=*max_element(dp,dp+V); printf("%d\n",ans); } }
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() #define fi first #define se second typedef vector<int>vint; typedef pair<int,int>pint; typedef vector<pint>vpint; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} int N,M; struct UnionFindTree{ vector<int>par,sz; UnionFindTree(int n){ par.resize(n); sz.resize(n); for(int i=0;i<n;i++){ par[i]=i; sz[i]=1; } } int find(int x){ return x==par[x]?x:par[x]=find(par[x]); } void unite(int x,int y){ x=find(x);y=find(y); if(x==y)return; if(sz[x]<sz[y])swap(x,y); sz[x]+=sz[y]; par[y]=x; } bool areSame(int x,int y){ return find(x)==find(y); } int size(int x){ return sz[find(x)]; } }; UnionFindTree uf(0); int dp[111111],dp2[111111]; vint G[111111]; void dfs(int v,int p,int r){ for(auto u:G[v]){ if(u==p)continue; if(uf.find(u)!=r){ chmax(dp2[v],dp[u]+1); } else{ dfs(u,v,r); chmax(dp2[v],dp2[u]+1); } } } void dfs2(int v,int p,int r,int x){ vint lis={x}; for(auto u:G[v]){ if(u==p)continue; if(uf.find(u)==r){ lis.pb(dp2[u]+1); } else{ lis.pb(dp[u]+1); } } sort(all(lis));reverse(all(lis)); dp[v]=lis[0]; for(auto u:G[v]){ if(u==p||uf.find(u)!=r)continue; int xx; if(lis[0]==dp2[u]+1)xx=lis[1]; else xx=lis[0]; dfs2(u,v,r,xx+1); } } signed main(){ cin>>N>>M; vpint es,es2; rep(i,M){ int a,b,w; cin>>a>>b>>w; a--;b--; if(w==1)es.pb({a,b}); else es2.pb({a,b}); } uf=UnionFindTree(N); for(auto &p:es2){ if(uf.areSame(p.fi,p.se)){ cout<<"Infinite"<<endl; return 0; } uf.unite(p.fi,p.se); } vector<vint>D(N); vint deg(N); for(auto &p:es){ D[uf.find(p.fi)].pb(uf.find(p.se)); deg[uf.find(p.se)]++; } vint ord; rep(i,N)if(uf.find(i)==i&&deg[i]==0)ord.pb(i); rep(i,ord.size()){ int v=ord[i]; for(auto u:D[v]){ if(--deg[u]==0)ord.pb(u); } } if(count(all(deg),0)!=N){ cout<<"Infinite"<<endl; return 0; } for(auto &p:es)G[p.fi].pb(p.se); for(auto &p:es2)G[p.fi].pb(p.se),G[p.se].pb(p.fi); for(int i=(int)ord.size()-1;i>=0;i--){ dfs(ord[i],-1,ord[i]); dfs2(ord[i],-1,ord[i],0); } cout<<*max_element(dp,dp+N)<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 int n,m,ans; int x[MAX_M],y[MAX_M],w[MAX_M]; vector<int> G[MAX_N]; vector<int> g[MAX_N]; vector<int> group[MAX_N]; int id[MAX_N]; bool visited[MAX_N]; vector<int> vd; int dp[MAX_N]; int d[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(to==prev)continue; if(i&&to==G[pos][i-1])return true; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++) if(!visited[i]&&dfs(i,-1,i))return true; vector<int> deg(n); for(int pos=0;pos<n;pos++) for(int to:g[pos]) deg[ id[to] ]++; queue<int> Q; for(int i=0;i<n;i++) if(deg[i]==0&&id[i]==i) Q.push(i); while(!Q.empty()){ int root=Q.front();Q.pop(); vd.push_back(root); for(int pos:group[root]){ for(int to:g[pos]){ deg[ id[to] ]--; if(deg[ id[to] ]==0)Q.push(id[to]); } } } for(int i=0;i<n;i++) if(deg[i]>0)return true; return false; } int solve(int S){ for(int pos:group[id[S]])d[pos]=-1; queue<int> Q; Q.push(S); d[S]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int to:G[pos]){ if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } int res=S; for(int pos:group[id[S]]){ if(d[pos]+dp[pos]>d[res]+dp[res])res=pos; ans=max(ans,d[pos]+dp[S]); for(int to:g[pos])dp[to]=max(dp[to],d[pos]+dp[S]+1); } return res; } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==1){ g[x[i]].push_back(y[i]); } if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if(check()){ printf("Infinite\n"); return 0; } for(int root : vd ) if(root==id[root]) solve(solve(solve(root))); printf("%d\n",ans); return 0; }
#include <string> #include <vector> #include<iostream> #include<cstdio> #include<cstdlib> #include<stack> #include<queue> #include<cmath> #include<algorithm> #include<functional> #include<list> #include<deque> #include<bitset> #include<set> #include<map> #include<unordered_map> #include<unordered_set> #include<cstring> #include<sstream> #include<complex> #include<iomanip> #include<numeric> #include<cassert> #define X first #define Y second #define pb push_back #define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X)) #define reps(X,S,Y) for (int (X) = S;(X) < (Y);++(X)) #define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X)) #define rreps(X,S,Y) for (int (X) = (Y)-1;(X) >= (S);--(X)) #define repe(X,Y) for ((X) = 0;(X) < (Y);++(X)) #define peat(X,Y) for (;(X) < (Y);++(X)) #define all(X) (X).begin(),(X).end() #define rall(X) (X).rbegin(),(X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)),(X).end()) #define Endl endl using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; template<class T> using vv=vector<vector<T>>; template<class T> ostream& operator<<(ostream &os, const vector<T> &t) { os<<"{"; rep(i,t.size()) {os<<t[i]<<",";} os<<"}"<<endl; return os;} template<class T,size_t n> ostream& operator<<(ostream &os, const array<T,n> &t) { os<<"{"; rep(i,n) {os<<t[i]<<",";} os<<"}"<<endl; return os;} template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";} template<class S, class T,class U> ostream& operator<<(ostream &os, const tuple<S,T,U> &t) { return os<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";} template<class S, class T,class U,class V> ostream& operator<<(ostream &os, const tuple<S,T,U,V> &t) { return os<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<","<<get<3>(t)<<")";} template<class S, class T,class U,class V,class W> ostream& operator<<(ostream &os, const tuple<S,T,U,V,W> &t) { return os<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<","<<get<3>(t)<<","<<get<4>(t)<<")";} template<class T> inline bool MX(T &l,const T &r){return l<r?l=r,1:0;} template<class T> inline bool MN(T &l,const T &r){return l>r?l=r,1:0;} //#undef NUIP #ifdef NUIP #define out(args...){vector<string> a_r_g_s=s_p_l_i_t(#args, ','); e_r_r(a_r_g_s.begin(), args); } vector<string> s_p_l_i_t(const string &s, char c){vector<string> v;int d=0,f=0;string t;for(char c:s){if(!d&&c==',')v.pb(t),t="";else t+=c;if(c=='\"'||c=='\'')f^=1;if(!f&&c=='(')++d;if(!f&&c==')')--d;}v.pb(t);return move(v);} void e_r_r(vector<string>::iterator it) {} template<typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args){ if(*it==" 1"||*it=="1") cerr<<endl; else cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ", "; e_r_r(++it, args...);} #else #define out #endif #ifdef __cpp_init_captures template<typename T>vector<T> table(int n, T v){ return vector<T>(n, v);} template <class... Args> auto table(int n, Args... args){auto val = table(args...); return vector<decltype(val)>(n, move(val));} #endif const ll MOD=1e9+7; struct UF{ vector<int> data; UF(int size):data(size,-1){} bool unite(int x,int y){ x=root(x); y=root(y); if(x!=y){ if(-data[y]>-data[x]) swap(x,y); data[x]+=data[y]; data[y]=x; } return x!=y; } bool findSet(int x,int y){return root(x)==root(y);} int root(int x){return data[x]<0?x:data[x]=root(data[x]);} int size(int x) {return -data[root(x)];} }; int main(){ ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(0); int n,m; cin>>n>>m; vv<int> g(n),h(n); vector<int> bi(n); UF uf(n); int ok=1; rep(i,m){ int a,b,c; cin>>a>>b>>c; --a; --b; if(c==1){ g[a].pb(b); }else{ bi[a]=bi[b]=1; h[a].pb(b); h[b].pb(a); if(!uf.unite(a,b)) ok=0; } } if(!ok){ cout<<"Infinite"<<Endl; return 0; } deque<int> que,todo; vector<int> usd(n),ind(n),cnt(n); rep(v,n)for(int w:g[v]) ++ind[w]; vv<int> mem(n); rep(i,n) mem[uf.root(i)].pb(i); rep(i,n)if(ind[i]==0 && MX(usd[i],1)){ if(bi[i]){ if(++cnt[uf.root(i)]==uf.size(i)){ todo.eb(uf.root(i)); } }else{ que.eb(i); } } vector<int> dp(n); vector<int> cs(n,-1),up(n,-1); function<int(int,int)> dfs= [&](int v,int p){ int mx=dp[v]; for(int w:h[v])if(w!=p) MX(mx,dfs(w,v)+1); return up[v]=mx; }; function<void(int,int,int)> dfs2= [&](int v,int p,int dn){ pii fst(dp[v],-1),snd(dn,p); if(fst<snd) swap(fst,snd); for(int w:h[v])if(w!=p){ pii p(up[w]+1,w); if(fst<p){ snd=fst; fst=p; }else if(snd<p){ snd=p; } } dp[v]=fst.X; for(int w:h[v])if(w!=p) dfs2(w,v,(fst.Y==w?snd.X:fst.X)+1); }; while(1){ while(que.size()){ int v=que.front(); que.pop_front(); out(v,dp[v],1); for(int w:g[v]){ MX(dp[w],dp[v]+1); if(--ind[w]==0 && MX(usd[w],1)){ if(bi[w]){ if(++cnt[uf.root(w)]==uf.size(w)){ todo.eb(uf.root(w)); } }else{ que.eb(w); } } } } if(todo.empty()) break; out(dp,1); while(todo.size()){ int x=todo.front(); todo.pop_front(); dfs(x,-1); out(x,up,1); dfs2(x,-1,0); for(int y:mem[x]) que.eb(y); } out(cs,dp,1); } out(cs,1); if(*min_element(all(usd))==0) ok=0; if(!ok){ cout<<"Infinite"<<Endl; return 0; } cout<<*max_element(all(dp))<<endl; return 0; }
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 100005 struct GROUP{ vector<int> nodes; }; struct Edge{ Edge(int arg_to,bool arg_is_directed){ to = arg_to; is_directed = arg_is_directed; } int to; bool is_directed; }; struct Info{ Info(int arg_node_id,int arg_adj_node){ node_id = arg_node_id; adj_node = arg_adj_node; } int node_id,adj_node; }; struct Data{ Data(){ node_id = 0; sum_cost = 0; } Data(int arg_node_id,ll arg_sum_cost){ node_id = arg_node_id; sum_cost = arg_sum_cost; } bool operator<(const struct Data &arg) const{ return sum_cost < arg.sum_cost; } int node_id; ll sum_cost; }; int V,E; GROUP group[NUM]; vector<Edge> G[NUM]; vector<int> DAG_G[NUM]; vector<int> rev_G[NUM]; vector<Info> Connect[NUM]; stack<int> S; bool check[NUM]; int table[NUM],in_num[NUM]; int group_index; ll max_node_cost[NUM],in_max[NUM]; void dfs(int node_id){ check[node_id] = true; for(int i = 0; i < G[node_id].size(); i++){ if(!check[G[node_id][i].to])dfs(G[node_id][i].to); } S.push(node_id); } void reverse_dfs(int node_id){ check[node_id] = true; group[group_index].nodes.push_back(node_id); table[node_id] = group_index; for(int i = 0; i < rev_G[node_id].size(); i++){ if(!check[rev_G[node_id][i]])reverse_dfs(rev_G[node_id][i]); } } ll dfs_first(int group_id,int node_id,int parent){ ll ret = in_max[node_id]; for(int i = 0; i < G[node_id].size(); i++){ if(table[G[node_id][i].to] != group_id || G[node_id][i].to == parent)continue; ret = max(ret,dfs_first(group_id,G[node_id][i].to,node_id)+1); } return max_node_cost[node_id] = ret; } void dfs_to_descendant(int group_id,int node_id,int parent,ll sum_cost){ max_node_cost[node_id] = max(max_node_cost[node_id],sum_cost); ll tmp_cost; priority_queue<Data> Q; for(int i = 0; i < G[node_id].size(); i++){ if(table[G[node_id][i].to] != group_id || G[node_id][i].to == parent)continue; Q.push(Data(G[node_id][i].to,max_node_cost[G[node_id][i].to]+1)); } if(Q.size() == 0){ tmp_cost = max(sum_cost,in_max[node_id])+1; for(int i = 0; i < G[node_id].size(); i++){ if(table[G[node_id][i].to] != group_id || G[node_id][i].to == parent)continue; dfs_to_descendant(group_id,G[node_id][i].to,node_id,tmp_cost); } }else if(Q.size() == 1){ Data data1 = Q.top(); tmp_cost = max(sum_cost,in_max[node_id])+1; for(int i = 0; i < G[node_id].size(); i++){ if(table[G[node_id][i].to] != group_id || G[node_id][i].to == parent)continue; if(G[node_id][i].to == data1.node_id){ dfs_to_descendant(group_id,G[node_id][i].to,node_id,tmp_cost); }else{ dfs_to_descendant(group_id,G[node_id][i].to,node_id,max(tmp_cost,data1.sum_cost+1)); } } }else{ tmp_cost = max(sum_cost,in_max[node_id])+1; Data data1 = Q.top(); Q.pop(); Data data2 = Q.top(); Q.pop(); for(int i = 0; i < G[node_id].size(); i++){ if(table[G[node_id][i].to] != group_id || G[node_id][i].to == parent)continue; if(G[node_id][i].to == data1.node_id){ dfs_to_descendant(group_id,G[node_id][i].to,node_id,max(tmp_cost,data2.sum_cost+1)); }else{ dfs_to_descendant(group_id,G[node_id][i].to,node_id,max(tmp_cost,data1.sum_cost+1)); } } } } void calc_cost(int group_id){ int tmp_node,adj_node; for(int i = 0; i < Connect[group_id].size(); i++){ tmp_node = Connect[group_id][i].node_id; adj_node = Connect[group_id][i].adj_node; in_max[tmp_node] = max(in_max[tmp_node],max_node_cost[adj_node]+1); } dfs_first(group_id,group[group_id].nodes[0],-1); dfs_to_descendant(group_id,group[group_id].nodes[0],-1,0); } int main(){ scanf("%d %d",&V,&E); int from,to,command; for(int loop = 0; loop < E; loop++){ scanf("%d %d %d",&from,&to,&command); from--; to--; if(command == 1){ G[from].push_back(Edge(to,true)); rev_G[to].push_back(from); }else{ G[from].push_back(Edge(to,false)); G[to].push_back(Edge(from,false)); rev_G[from].push_back(to); rev_G[to].push_back(from); } } for(int i = 0; i < V; i++)check[i] = false; for(int i = 0; i < V; i++){ if(!check[i]){ dfs(i); } } for(int i = 0; i < V; i++)check[i] = false; group_index = 0; while(!S.empty()){ if(!check[S.top()]){ reverse_dfs(S.top()); group_index++; } S.pop(); } int node_num,edge_num; int tmp_node,next_group; for(int i = 0; i < group_index; i++){ node_num = (int)group[i].nodes.size(); edge_num = 0; for(int k = 0; k < group[i].nodes.size(); k++){ tmp_node = group[i].nodes[k]; for(int p = 0; p < G[tmp_node].size(); p++){ next_group = table[G[tmp_node][p].to]; if(next_group == i){ if(G[tmp_node][p].is_directed){ printf("Infinite\n"); return 0; } edge_num++; }else{ DAG_G[i].push_back(next_group); } } } edge_num /= 2; if(edge_num >= node_num){ printf("Infinite\n"); return 0; } for(int k = 0; k < group[i].nodes.size(); k++){ tmp_node = group[i].nodes[k]; for(int p = 0; p < rev_G[tmp_node].size(); p++){ next_group = table[rev_G[tmp_node][p]]; if(next_group == i){ //Do nothing }else{ Connect[i].push_back(Info(tmp_node,rev_G[tmp_node][p])); } } } } for(int i = 0; i < group_index; i++)in_num[i] = 0; for(int i = 0; i < group_index; i++){ sort(DAG_G[i].begin(),DAG_G[i].end()); DAG_G[i].erase(unique(DAG_G[i].begin(),DAG_G[i].end()),DAG_G[i].end()); for(int k = 0; k < DAG_G[i].size(); k++){ in_num[DAG_G[i][k]]++; } } for(int i = 0; i < V; i++){ max_node_cost[i] = 0; in_max[i] = 0; } queue<int> Q; for(int i = 0; i < group_index; i++){ if(in_num[i] == 0){ Q.push(i); } } int tmp_group; while(!Q.empty()){ tmp_group = Q.front(); Q.pop(); calc_cost(tmp_group); for(int i = 0; i < DAG_G[tmp_group].size(); i++){ in_num[DAG_G[tmp_group][i]] -= 1; if(in_num[DAG_G[tmp_group][i]] == 0){ Q.push(DAG_G[tmp_group][i]); } } } ll ans = 0; for(int i = 0; i < V; i++){ ans = max(ans,max_node_cost[i]); } printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector< pair< int, int > > g[100001]; vector< int > leftt[100001], rightt[100001], just[100001]; vector< int > gg[100001], rgg[100001], order; bool v[100000]; int cmp[100000]; void dfs(int idx) { if(v[idx]++) return; for(auto &to : gg[idx]) dfs(to); order.push_back(idx); } void rdfs(int idx, int k) { cmp[idx] = k; for(auto &to : rgg[idx]) if(cmp[to] == -1) rdfs(to, k); } int rec(int idx, int back = -1) { if(back != -1) { int tmp = max(leftt[idx][back], rightt[idx][back + 1]); if(tmp != INF) return (tmp); } if(rightt[idx][0] == INF) { for(int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if(i == back) continue; just[idx][i] = rec(to, rev) + 1; } for(int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for(int i = (int) g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if(back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector< pair< int, int > > edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for(int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if(t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if(uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for(auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); rgg[uf.find(e.second)].push_back(uf.find(e.first)); } for(int i = 0; i < N; i++) dfs(i); reverse(begin(order), end(order)); memset(cmp, -1, sizeof(cmp)); int kk = 0; for(auto &i : order) if(cmp[i] == -1) rdfs(i, kk++); if(kk != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for(int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for(int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 int n,m; int x[MAX_M],y[MAX_M],w[MAX_M]; vector<int> G[MAX_N]; vector<int> g[MAX_N]; vector<int> group[MAX_N]; int id[MAX_N]; bool visited[MAX_N]; vector<int> vd; int dp[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(to==prev)continue; if(i&&to==G[pos][i-1])return true; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++){ if(visited[i])continue; if( dfs(i,-1,i) )return true; } vector< int > deg(n); vector< vector<int> > graph(n); for(int i=0;i<m;i++){ if(w[i]==1){ int X=id[ x[i] ]; int Y=id[ y[i] ]; if(X==Y)return true; graph[X].push_back(Y); deg[Y]++; } } queue<int> Q; for(int i=0;i<n;i++) if(deg[i]==0&&id[i]==i) Q.push(i); while(!Q.empty()){ int pos=Q.front();Q.pop(); vd.push_back(pos); for(int to:graph[pos]){ deg[to]--; if(deg[to]==0)Q.push(to); } } for(int i=0;i<n;i++) if(deg[i]>0)return true; return false; } int d[MAX_N]; void init(int root){ root=id[root]; for(int pos:group[root]) d[pos]=-1; } void bfs(int S){ init(S); queue<int> Q; Q.push(S); d[S]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int to:G[pos]){ if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } } int solve(int S,int &ans){ bfs(S); int res=S; for(int pos:group[id[S]]){ if(d[pos]+dp[pos]>d[res]+dp[res])res=pos; ans=max(ans,d[pos]+dp[S]); for(int to:g[pos])dp[to]=max(dp[to],d[pos]+dp[S]+1); } return res; } /* int res=0; bfs(root); int maxm=-1,maxmid; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); maxm=-1; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); return res; */ int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==1){ g[x[i]].push_back(y[i]); } if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if( check() ){ printf("Infinite\n"); return 0; } int ans=0; for(int root : vd ){ if(root!=id[root])continue; solve( solve( solve(root,ans) , ans ) , ans); } printf("%d\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 typedef pair<int,int> P; int n,m; vector<int> group[MAX_N]; vector<int> G[MAX_N]; int x[MAX_M],y[MAX_M],w[MAX_M]; int id[MAX_N]; bool visited[MAX_N]; int c[MAX_N]; vector<int> g[MAX_N]; vector<int> vd; int dp[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int to:G[pos]){ if(to==prev)continue; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ map<P,bool> mp; for(int i=0;i<m;i++){ if(w[i]==1)continue; if(x[i]>y[i])swap(x[i],y[i]); if(mp[ P(x[i],y[i]) ])return true; mp[ P(x[i],y[i]) ]=true; } memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++){ if(visited[i])continue; if( dfs(i,-1,i) )return true; } for(int i=0;i<m;i++){ if(w[i]==2)continue; int X=id[ x[i] ]; int Y=id[ y[i] ]; if(X==Y){ return true; } g[X].push_back(Y); c[Y]++; } queue<int> Q; for(int i=0;i<n;i++){ if(c[i]==0&&id[i]==i){ Q.push(i); } } while(!Q.empty()){ int pos=Q.front();Q.pop(); vd.push_back(pos); for(int to:g[pos]){ c[to]--; if(c[to]==0)Q.push(to); } } for(int i=0;i<n;i++) if(c[i]>0)return true; return false; } int d[MAX_N]; void init(int root){ root=id[root]; for(int pos:group[root]){ d[pos]=-1; } } void bfs(int root){ init(root); queue<int> Q; Q.push(root); d[root]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int to:G[pos]){ if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } } void update(int root,int dif){ for(int pos:group[root]){ for(int to:g[pos]){ dp[to]=max(dp[to],d[pos]+dif+1); } } } int solve(int root){ int res=0; bfs(root); int maxm=-1,maxmid; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); maxm=-1; for(int pos:group[root]) if(d[pos]+dp[pos]>maxm)maxm=d[pos]+dp[pos],maxmid=pos; bfs(maxmid); update(root,dp[maxmid]); for(int pos:group[root])res=max(res,d[pos]+dp[maxmid]); return res; } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if( check() ){ printf("Infinite\n"); return 0; } for(int i=0;i<n;i++){ g[i].clear(); } for(int i=0;i<m;i++){ if(w[i]==1){ g[ x[i] ].push_back(y[i]); } } int ans=0; for(int root : vd ){ if(root!=id[root])continue; ans=max(ans,solve(root)); } printf("%d\n",ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define MAX_N 100005 #define MAX_M 100005 int n,m,ans; int x[MAX_M],y[MAX_M],w[MAX_M]; vector<int> G[MAX_N]; vector<int> g[MAX_N]; vector<int> group[MAX_N]; int id[MAX_N]; bool visited[MAX_N]; vector<int> vd; int dp[MAX_N]; int d[MAX_N]; bool dfs(int pos,int prev,int root){ id[pos]=root; visited[pos]=true; group[root].push_back(pos); for(int i=0;i<(int)G[pos].size();i++){ int to=G[pos][i]; if(to==prev)continue; if(i&&to==G[pos][i-1])return true; if(visited[to])return true; if(dfs(to,pos,root))return true; } return false; } bool check(){ memset(visited,false,sizeof(visited)); for(int i=0;i<n;i++) if(!visited[i]&&dfs(i,-1,i))return true; vector< int > deg(n); vector< vector<int> > graph(n); for(int i=0;i<m;i++){ if(w[i]==1){ int X=id[ x[i] ]; int Y=id[ y[i] ]; if(X==Y)return true; graph[X].push_back(Y); deg[Y]++; } } queue<int> Q; for(int i=0;i<n;i++) if(deg[i]==0&&id[i]==i) Q.push(i); while(!Q.empty()){ int pos=Q.front();Q.pop(); vd.push_back(pos); for(int to:graph[pos]){ deg[to]--; if(deg[to]==0)Q.push(to); } } for(int i=0;i<n;i++) if(deg[i]>0)return true; return false; } int solve(int S){ for(int pos:group[id[S]])d[pos]=-1; queue<int> Q; Q.push(S); d[S]=0; while(!Q.empty()){ int pos=Q.front();Q.pop(); for(int to:G[pos]){ if(d[to]!=-1)continue; d[to]=d[pos]+1; Q.push(to); } } int res=S; for(int pos:group[id[S]]){ if(d[pos]+dp[pos]>d[res]+dp[res])res=pos; ans=max(ans,d[pos]+dp[S]); for(int to:g[pos])dp[to]=max(dp[to],d[pos]+dp[S]+1); } return res; } int main(){ scanf("%d %d",&n,&m); for(int i=0;i<m;i++){ scanf("%d %d %d",&x[i],&y[i],&w[i]); x[i]--,y[i]--; if(w[i]==1){ g[x[i]].push_back(y[i]); } if(w[i]==2){ G[x[i]].push_back(y[i]); G[y[i]].push_back(x[i]); } } if(check()){ printf("Infinite\n"); return 0; } for(int root : vd ) if(root==id[root]) solve(solve(solve(root))); printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if(x == y) return; if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector< pair< int, int > > g[100001]; vector< int > leftt[100001], rightt[100001], just[100001]; vector< int > gg[100001]; int deg[100001]; int rec(int idx, int back = -1) { if(back != -1) { int tmp = max(leftt[idx][back], rightt[idx][back + 1]); if(tmp != INF) return (tmp); } if(rightt[idx][0] == INF) { for(int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if(i == back) continue; just[idx][i] = rec(to, rev) + 1; } for(int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for(int i = (int) g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if(back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector< pair< int, int > > edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for(int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if(t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if(uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for(auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); ++deg[uf.find(e.second)]; } vector< int > order; for(int i = 0; i < N; i++) { if(deg[i] == 0) order.push_back(i); } for(int i = 0; i < order.size(); i++) { for(auto &e : gg[order[i]]) { if(--deg[e] == 0) order.push_back(e); } } if(order.size() != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for(int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for(int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }