Skip to content

Instantly share code, notes, and snippets.

@kusano
Created May 30, 2015 16:38
Show Gist options
  • Save kusano/7bf1df1efb05d1868a0f to your computer and use it in GitHub Desktop.
Save kusano/7bf1df1efb05d1868a0f to your computer and use it in GitHub Desktop.
Google Code Jam 2015 Round 2
def solve(R, C, B):
ans = 0
for cy in range(R):
for cx in range(C):
if B[cy][cx]!=".":
ok = []
for d in range(4):
x = cx
y = cy
while True:
x += [0,1,0,-1][d]
y += [-1,0,1,0][d]
if x<0 or C<=x or y<0 or R<=y:
f = False
break
if B[y][x]!='.':
f = True
break
ok += [f]
if not any(ok):
return "IMPOSSIBLE"
if not ok["^>v<".index(B[cy][cx])]:
ans += 1
return ans
for t in range(input()):
R,C = map(int, raw_input().split())
B = [raw_input() for _ in range(R)]
print "Case #%s: %s" % (t+1, solve(R,C,B))
def solve(N, V, X, R, C):
if N==1:
if X==C[0]:
return "%.10f"%(V/R[0])
else:
return "IMPOSSIBLE"
elif N==2:
if min(C) <= X <= max(C):
if abs(C[1]-C[0]) > 0.:
v = V * abs(X-C[1]) / abs(C[1]-C[0])
return "%.10f" % max(v/R[0], (V-v)/R[1])
else:
return "%.10f" % (V/sum(R))
else:
return "IMPOSSIBLE"
else:
return "???"
for t in range(input()):
N,V,X = map(float, raw_input().split())
N = int(N)
R = [0.]*N
C = [0.]*N
for i in range(N):
R[i],C[i] = map(float, raw_input().split())
print "Case #%s: %s" % (t+1, solve(N,V,X,R,C))
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <algorithm>
using namespace std;
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
int main()
{
int T;
cin>>T;
for (int t=1; t<=T; t++)
{
int N;
cin>>N;
string tmp;
getline(cin, tmp);
vector<vector<string>> S(N);
set<string> D;
for (int i=0; i<N; i++)
{
string s;
getline(cin, s);
stringstream ss(s);
string a;
while (ss>>a)
{
S[i].push_back(a);
D.insert(a);
}
}
vector<vector<int>> A;
int base = 0;
for (string s: D)
{
vector<int> T;
for (int i=0; i<N; i++)
for (string a: S[i])
if (a==s)
{
T.push_back(i);
break;
}
if (T.size()>=2 && T[0]==0 && T[1]==1)
{
base++;
}
else if (T.back() >= 2)
A.push_back(T);
}
int ans = 99999999;
for (int b=0; b<(1<<N); b++)
if ((b&3)==1)
{
int c = 0;
for (vector<int> &a: A)
{
int f = 0;
for (int x: a)
f |= 1<<(b>>x&1);
if (f==3)
c++;
}
ans = min(ans, c+base);
}
cout << "Case #" << t << ": " << ans << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment