puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 036

C++ solution:

Crystal solution: n, q = read_line.split.map(&.to_i)
x, y = (1..n).map {
  a, b = read_line.split.map(&.to_i64)
  {a - b, a + b}
}.transpose
xy = x.minmax + y.minmax

q.times do
  i = read_line.to_i - 1
  puts [x[i], x[i], y[i], y[i]].zip(xy).max_of { |a, b| (a - b).abs }
end





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var x: []int :: #[n]int
	var y: []int :: #[n]int
	for i(0, n - 1)
		do x[i] :: cui@inputInt()
		do y[i] :: cui@inputInt()
		var nx: int :: x[i] - y[i]
		var ny: int :: x[i] + y[i]
		do x[i] :: nx
		do y[i] :: ny
	end for
	
	var minX: int :: x[0]
	var maxX: int :: x[0]
	var minY: int :: y[0]
	var maxY: int :: y[0]
	for i(1, n - 1)
		do minX :: [minX, x[i]].min()
		do maxX :: [maxX, x[i]].max()
		do minY :: [minY, y[i]].min()
		do maxY :: [maxY, y[i]].max()
	end for
	
	for(1, q)
		var idx: int :: cui@inputInt() - 1
		var ans: int :: 0
		do ans :: [ans, (x[idx] - minX).abs()].max()
		do ans :: [ans, (x[idx] - maxX).abs()].max()
		do ans :: [ans, (y[idx] - minY).abs()].max()
		do ans :: [ans, (y[idx] - maxY).abs()].max()
		do cui@print("\{ans}\n")
	end for
end func
AtCoder problem 038

C++ solution:
#include <iostream>

using namespace std;



long long gcd(long long a, long long b) {

	if (b == 0) return a;

	return gcd(b, a % b);

}



int main() {

	long long A, B, THRESHOLD = 1000000000000000000LL;

	cin >> A >> B;



	long long C = A / gcd(A, B);

	if (B <= THRESHOLD / C) cout << C * B << endl;

	else cout << "Large" << endl;

	return 0;

}

Crystal solution: require "big"
a, b = read_line.split.map(&.to_big_i)
lcm = a.lcm(b)
puts lcm <= 10i64**18 ? lcm : "Large"
AtCoder problem 038

C++ solution:

Crystal solution: require "big"
a, b = read_line.split.map(&.to_big_i)
lcm = a.lcm(b)
puts lcm <= 10i64**18 ? lcm : "Large"


Python solution: from math import gcd

a, b = map(int, input().split())

ans = a//gcd(a, b)*b

if ans > int(pow(10, 18)):
    print('Large')
else:
    print(ans)
AtCoder problem 038

C++ solution:

Crystal solution: require "big"
a, b = read_line.split.map(&.to_big_i)
lcm = a.lcm(b)
puts lcm <= 10i64**18 ? lcm : "Large"






Ruby solution: MAX = 10 ** 18

a, b = readline.split.map(&:to_i)
lcm = a.lcm(b)
puts (lcm <= MAX) ? lcm : 'Large'
AtCoder problem 038

C++ solution:

Crystal solution: require "big"
a, b = read_line.split.map(&.to_big_i)
lcm = a.lcm(b)
puts lcm <= 10i64**18 ? lcm : "Large"





Kuin solution: func main()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var v: int :: b / math@gcd(a, b)
	var ans: int :: a
	if(ans > 10 ^ 18 / v)
		do cui@print("Large\n")
		ret
	end if
	do ans :* v
	do cui@print("\{ans}\n")
end func
AtCoder problem 038

C++ solution:
#include <iostream>

using namespace std;



long long gcd(long long a, long long b) {

	if (b == 0) return a;

	return gcd(b, a % b);

}



int main() {

	long long A, B, THRESHOLD = 1000000000000000000LL;

	cin >> A >> B;



	long long C = A / gcd(A, B);

	if (B <= THRESHOLD / C) cout << C * B << endl;

	else cout << "Large" << endl;

	return 0;

}


Python solution: from math import gcd

a, b = map(int, input().split())

ans = a//gcd(a, b)*b

if ans > int(pow(10, 18)):
    print('Large')
else:
    print(ans)
AtCoder problem 038

C++ solution:
#include <iostream>

using namespace std;



long long gcd(long long a, long long b) {

	if (b == 0) return a;

	return gcd(b, a % b);

}



int main() {

	long long A, B, THRESHOLD = 1000000000000000000LL;

	cin >> A >> B;



	long long C = A / gcd(A, B);

	if (B <= THRESHOLD / C) cout << C * B << endl;

	else cout << "Large" << endl;

	return 0;

}






Ruby solution: MAX = 10 ** 18

a, b = readline.split.map(&:to_i)
lcm = a.lcm(b)
puts (lcm <= MAX) ? lcm : 'Large'
AtCoder problem 038

C++ solution:
#include <iostream>

using namespace std;



long long gcd(long long a, long long b) {

	if (b == 0) return a;

	return gcd(b, a % b);

}



int main() {

	long long A, B, THRESHOLD = 1000000000000000000LL;

	cin >> A >> B;



	long long C = A / gcd(A, B);

	if (B <= THRESHOLD / C) cout << C * B << endl;

	else cout << "Large" << endl;

	return 0;

}





Kuin solution: func main()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var v: int :: b / math@gcd(a, b)
	var ans: int :: a
	if(ans > 10 ^ 18 / v)
		do cui@print("Large\n")
		ret
	end if
	do ans :* v
	do cui@print("\{ans}\n")
end func
AtCoder problem 038

C++ solution:


Python solution: from math import gcd

a, b = map(int, input().split())

ans = a//gcd(a, b)*b

if ans > int(pow(10, 18)):
    print('Large')
else:
    print(ans)





Ruby solution: MAX = 10 ** 18

a, b = readline.split.map(&:to_i)
lcm = a.lcm(b)
puts (lcm <= MAX) ? lcm : 'Large'
AtCoder problem 038

C++ solution:


Python solution: from math import gcd

a, b = map(int, input().split())

ans = a//gcd(a, b)*b

if ans > int(pow(10, 18)):
    print('Large')
else:
    print(ans)




Kuin solution: func main()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var v: int :: b / math@gcd(a, b)
	var ans: int :: a
	if(ans > 10 ^ 18 / v)
		do cui@print("Large\n")
		ret
	end if
	do ans :* v
	do cui@print("\{ans}\n")
end func
AtCoder problem 038

C++ solution:





Kuin solution: func main()
	var a: int :: cui@inputInt()
	var b: int :: cui@inputInt()
	var v: int :: b / math@gcd(a, b)
	var ans: int :: a
	if(ans > 10 ^ 18 / v)
		do cui@print("Large\n")
		ret
	end if
	do ans :* v
	do cui@print("\{ans}\n")
end func


Ruby solution: MAX = 10 ** 18

a, b = readline.split.map(&:to_i)
lcm = a.lcm(b)
puts (lcm <= MAX) ? lcm : 'Large'
AtCoder problem 039

C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;



long long N;

long long A[1 << 18], B[1 << 18];

long long dp[1 << 18];

vector<int> G[1 << 18];



void dfs(int pos, int pre) {

	dp[pos] = 1;

	for (int i : G[pos]) {

		if (i == pre) continue;

		dfs(i, pos);

		dp[pos] += dp[i];

	}

}



int main() {

	// Step #1. “ü—Í

	cin >> N;

	for (int i = 1; i <= N - 1; i++) {

		cin >> A[i] >> B[i];

		G[A[i]].push_back(B[i]);

		G[B[i]].push_back(A[i]);

	}



	// Step #2. �[‚³—D�æ’T�õ�iDFS�j

	dfs(1, -1);



	// Step #3. “š‚¦‚ğ‹�‚ß‚é

	long long Answer = 0;

	for (int i = 1; i <= N - 1; i++) {

		long long r = min(dp[A[i]], dp[B[i]]);

		Answer += r * (N - r);

	}

	cout << Answer << endl;

	return 0;

}

Crystal solution: class UnweightedGraph
  getter graph : Array(Array(Int32))

  def initialize(size : Int)
    @graph = Array.new(size) { Array(Int32).new }
  end

  def add_edge(v, u)
    @graph[v] << u
    @graph[u] << v
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  private def subtree_size_dfs(v : Int32, p : Int32, result : Array(Int32)) : Int32
    result[v] = 1 + self[v].sum do |u|
      u != p ? subtree_size_dfs(u, v, result) : 0
    end
  end

  def subtree_size(root : Int32)
    result = Array.new(size, 0)
    subtree_size_dfs(root, -1, result)
    result
  end
end

n = read_line.to_i
graph = UnweightedGraph.new(n)
n.pred.times do
  a, b = read_line.split.map(&.to_i.pred)
  graph.add_edge(a, b)
end
puts graph.subtree_size(0).skip(1).sum { |size|
  size.to_i64 * (n - size)
}
AtCoder problem 039

C++ solution:

Crystal solution: class UnweightedGraph
  getter graph : Array(Array(Int32))

  def initialize(size : Int)
    @graph = Array.new(size) { Array(Int32).new }
  end

  def add_edge(v, u)
    @graph[v] << u
    @graph[u] << v
  end

  delegate size, to: @graph
  delegate :[], to: @graph

  private def subtree_size_dfs(v : Int32, p : Int32, result : Array(Int32)) : Int32
    result[v] = 1 + self[v].sum do |u|
      u != p ? subtree_size_dfs(u, v, result) : 0
    end
  end

  def subtree_size(root : Int32)
    result = Array.new(size, 0)
    subtree_size_dfs(root, -1, result)
    result
  end
end

n = read_line.to_i
graph = UnweightedGraph.new(n)
n.pred.times do
  a, b = read_line.split.map(&.to_i.pred)
  graph.add_edge(a, b)
end
puts graph.subtree_size(0).skip(1).sum { |size|
  size.to_i64 * (n - size)
}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var a: []int :: #[n]int
	do dfs(graph, 0, -1, a)
	var ans: int :: 0
	for u(0, n - 1)
		do graph[u].head()
		while(!graph[u].term())
			var v: int :: graph[u].get()
			var min: int :: [a[u], a[v]].min()
			do ans :+ min * (n - min)
			do graph[u].next()
		end while
	end for
	do ans :/ 2
	do cui@print("\{ans}\n")
	
	func dfs(graph: []list<int>, u: int, p: int, a: []int)
		do a[u] :: 1
		do graph[u].head()
		while(!graph[u].term())
			var v: int :: graph[u].get()
			if(v <> p)
				do dfs(graph, v, u, a)
				do a[u] :+ a[v]
			end if
			do graph[u].next()
		end while
	end func
end func
AtCoder problem 039

C++ solution:
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;



long long N;

long long A[1 << 18], B[1 << 18];

long long dp[1 << 18];

vector<int> G[1 << 18];



void dfs(int pos, int pre) {

	dp[pos] = 1;

	for (int i : G[pos]) {

		if (i == pre) continue;

		dfs(i, pos);

		dp[pos] += dp[i];

	}

}



int main() {

	// Step #1. “ü—Í

	cin >> N;

	for (int i = 1; i <= N - 1; i++) {

		cin >> A[i] >> B[i];

		G[A[i]].push_back(B[i]);

		G[B[i]].push_back(A[i]);

	}



	// Step #2. �[‚³—D�æ’T�õ�iDFS�j

	dfs(1, -1);



	// Step #3. “š‚¦‚ğ‹�‚ß‚é

	long long Answer = 0;

	for (int i = 1; i <= N - 1; i++) {

		long long r = min(dp[A[i]], dp[B[i]]);

		Answer += r * (N - r);

	}

	cout << Answer << endl;

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var graph: []list<int> :: #[n]list<int>
	for i(0, n - 1)
		do graph[i] :: #list<int>
	end for
	for i(0, n - 2)
		var a: int :: cui@inputInt() - 1
		var b: int :: cui@inputInt() - 1
		do graph[a].add(b)
		do graph[b].add(a)
	end for
	
	var a: []int :: #[n]int
	do dfs(graph, 0, -1, a)
	var ans: int :: 0
	for u(0, n - 1)
		do graph[u].head()
		while(!graph[u].term())
			var v: int :: graph[u].get()
			var min: int :: [a[u], a[v]].min()
			do ans :+ min * (n - min)
			do graph[u].next()
		end while
	end for
	do ans :/ 2
	do cui@print("\{ans}\n")
	
	func dfs(graph: []list<int>, u: int, p: int, a: []int)
		do a[u] :: 1
		do graph[u].head()
		while(!graph[u].term())
			var v: int :: graph[u].get()
			if(v <> p)
				do dfs(graph, v, u, a)
				do a[u] :+ a[v]
			end if
			do graph[u].next()
		end while
	end func
end func
AtCoder problem 040

C++ solution:
// Time Complexity: O(N^4 log W)



#include <vector>

#include <iostream>

#include <algorithm>

#include <functional>

using namespace std;

const int inf = 1012345678;

class edge {

public:

	int to, cap, rev;

	edge() : to(-1), cap(0), rev(-1) {};

	edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_) {};

};

bool find_augment(int pos, int tar, int step, vector<vector<edge> >& G, vector<bool>& vis) {

	if (pos == tar) return true;

	vis[pos] = true;

	for (edge& e : G[pos]) {

		if (!vis[e.to] && e.cap >= step) {

			bool res = find_augment(e.to, tar, step, G, vis);

			if (res) {

				e.cap -= step;

				G[e.to][e.rev].cap += step;

				return true;

			}

		}

	}

	return false;

}

int max_flow(int src, int tar, int maxstep, vector<vector<edge> >& G) {

	int flow = 0;

	int step = 1;

	while (step * 2 <= maxstep) {

		step *= 2;

	}

	while (true) {

		vector<bool> vis(G.size(), false);

		bool res = find_augment(src, tar, step, G, vis);

		if (!res) {

			if (step == 1) break;

			step >>= 1;

		}

		else {

			flow += step;

		}

	}

	return flow;

}

int main() {

	int N, W;

	cin >> N >> W;

	vector<int> A(N);

	for (int i = 0; i < N; ++i) {

		cin >> A[i];

	}

	vector<vector<edge> > G(N + 2);

	function<void(int, int, int)> add_edge = [&](int va, int vb, int cap) {

		G[va].push_back(edge(vb, cap, G[vb].size()));

		G[vb].push_back(edge(va, 0, G[va].size() - 1));

	};

	for (int i = 1; i <= N; ++i) {

		int K, c;

		cin >> K;

		for (int j = 0; j < K; ++j) {

			cin >> c;

			add_edge(c, i, inf);

		}

	}

	for (int i = 1; i <= N; ++i) {

		add_edge(0, i, A[i - 1]);

		add_edge(i, N + 1, W);

	}

	int res = max_flow(0, N + 1, W, G);

	int answer = -res;

	for (int i = 0; i < N; ++i) {

		answer += A[i];

	}

	cout << answer << endl;

	return 0;

}


Python solution: from networkx import*
(n,w),A,*z=[[*map(int,t.split())]for t in open(0)]
g=DiGraph()
d=g.add_edge
while n:
 n-=1;d(1,~n,c=w);d(~n,2,c=A[n])
 for p in z[n][1:]:d(~n,-p,c=9e9)
print(sum(A)-minimum_cut(g,1,2,capacity='c')[0])
AtCoder problem 042

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

const int mod = 1000000007;

int main() {

	int K;

	cin >> K;

	if (K % 9 == 0) {

		vector<int> dp(K + 1);

		dp[0] = 1;

		for (int i = 1; i <= K; ++i) {

			for (int j = i - 1; j >= i - 9 && j >= 0; --j) {

				dp[i] += dp[j];

				if (dp[i] >= mod) dp[i] -= mod;

			}

		}

		cout << dp[K] << endl;

	}

	else {

		cout << 0 << endl;

	}

	return 0;

}

Crystal solution: k = read_line.to_i
puts k % 9 == 0 ? k.times.each_with_object([1i64]) { |_, dp|
  dp << dp.reverse_each.first(9).sum % (10**9 + 7)
}.last : 0
AtCoder problem 042

C++ solution:
#include <vector>

#include <iostream>

using namespace std;

const int mod = 1000000007;

int main() {

	int K;

	cin >> K;

	if (K % 9 == 0) {

		vector<int> dp(K + 1);

		dp[0] = 1;

		for (int i = 1; i <= K; ++i) {

			for (int j = i - 1; j >= i - 9 && j >= 0; --j) {

				dp[i] += dp[j];

				if (dp[i] >= mod) dp[i] -= mod;

			}

		}

		cout << dp[K] << endl;

	}

	else {

		cout << 0 << endl;

	}

	return 0;

}





Kuin solution: func main()
	var k: int :: cui@inputInt()
	if(k % 9 <> 0)
		do cui@print("0\n")
		ret
	end if
	var dp: []int :: #[k + 9]int
	do dp[0] :: 1
	for i(0, k - 1)
		for j(1, 9)
			do dp[i + j] :+ dp[i]
			do dp[i + j] :% 10 ^ 9 + 7
		end for
	end for
	
	var ans: int :: dp[k]
	do cui@print("\{ans}\n")
end func
AtCoder problem 042

C++ solution:

Crystal solution: k = read_line.to_i
puts k % 9 == 0 ? k.times.each_with_object([1i64]) { |_, dp|
  dp << dp.reverse_each.first(9).sum % (10**9 + 7)
}.last : 0





Kuin solution: func main()
	var k: int :: cui@inputInt()
	if(k % 9 <> 0)
		do cui@print("0\n")
		ret
	end if
	var dp: []int :: #[k + 9]int
	do dp[0] :: 1
	for i(0, k - 1)
		for j(1, 9)
			do dp[i + j] :+ dp[i]
			do dp[i + j] :% 10 ^ 9 + 7
		end for
	end for
	
	var ans: int :: dp[k]
	do cui@print("\{ans}\n")
end func
AtCoder problem 043

C++ solution:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef struct edge{
	long long to;
	long long cost;
}Edge;
typedef pair<long,long> P;

long long INF=1000000000000000000;
int V;
vector<Edge> G[2000000];
long long d[2000000];

void dijkstra(int s){
  priority_queue<P,vector<P>,greater<P>> que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();que.pop();
		int v=p.second;
		if(d[v]<p.first)continue;
		for(unsigned int i=0;i<G[v].size();i++){
		  Edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
			  d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}


void G_push(int st,int to,int w){
  G[st].push_back({to,w});
  G[to].push_back({st,w});
  return;
}

int main(){
  int H,W,sx,sy,gx,gy,N;
  cin>>H>>W>>sx>>sy>>gx>>gy;
  sx--;sy--;gx--;gy--;
  N=W*H;
  V=2*N;
  vector<string> C(H);
  for(int i=0;i<H;i++)cin>>C.at(i);
  for(int i=0;i<H;i++)for(int j=0;j<W;j++){
    if(C.at(i).at(j)=='#')continue;
    if(i>0 && C.at(i-1).at(j)=='.')G_push(W*i+j,W*(i-1)+j,0);
    if(j>0 && C.at(i).at(j-1)=='.')G_push(W*i+j+N,W*i+j-1+N,0);
    G_push(W*i+j,W*i+j+N,1);
  }
  G_push(W*sx+sy,W*sx+sy+N,0);
  dijkstra(W*sx+sy);
  printf("%lld\n",min(d[W*gx+gy],d[W*gx+gy+N]));
  return 0;
}


Crystal solution: struct Point
  Direction = {Point.new(0, 1), Point.new(1, 0), Point.new(0, -1), Point.new(-1, 0)}

  property y : Int32
  property x : Int32

  def initialize(@y, @x)
  end

  def +(other : Point)
    Point.new(y + other.y, x + other.x)
  end

  def in_range?(h, w)
    (0...h).includes?(y) && (0...w).includes?(x)
  end
end

struct Vertex
  getter p : Point
  getter d : Int32

  def initialize(@p, @d)
  end

  def to_i(w)
    p.y * w * 4 + p.x * 4 + d
  end
end

h, w = read_line.split.map(&.to_i)
start, goal = {0, 1}.map {
  y, x = read_line.split.map(&.to_i.pred)
  Point.new(y, x)
}
s = (1..h).map { read_line }

dist = Array(Int32?).new(h * w * 4, nil)
deque = Deque({Vertex, Int32}).new
(0...4).each do |d|
  e = Vertex.new(start, d)
  dist[e.to_i(w)] = 0
  deque << {e, 0}
end

add_edge = ->(dist_from : Int32, to : Vertex, cost : Int32) {
  dist_to = dist[to.to_i(w)]
  if dist_to.nil? || dist_to > dist_from + cost
    dist_to = dist_from + cost
    dist[to.to_i(w)] = dist_to
    if cost == 0
      deque.push({to, dist_to})
    else
      deque.unshift({to, dist_to})
    end
  end
}

until deque.empty?
  v, dist_now = deque.pop
  dist_from = dist[v.to_i(w)].not_nil!
  next if dist_from < dist_now

  d1 = v.d
  dir = Point::Direction[v.d]
  p1, p2 = v.p, v.p + dir

  if p2.in_range?(h, w) && s[p2.y][p2.x] == '.'
    add_edge.call(dist_from, Vertex.new(p2, d1), 0)
  end
  {d1 - 1, d1 + 1}.each do |d2|
    add_edge.call(dist_from, Vertex.new(p1, d2 % 4), 1)
  end
end

puts (0...4).min_of { |d|
  dist[Vertex.new(goal, d).to_i(w)].not_nil!
}
AtCoder problem 043

C++ solution:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;

typedef struct edge{
	long long to;
	long long cost;
}Edge;
typedef pair<long,long> P;

long long INF=1000000000000000000;
int V;
vector<Edge> G[2000000];
long long d[2000000];

void dijkstra(int s){
  priority_queue<P,vector<P>,greater<P>> que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));
	while(!que.empty()){
		P p=que.top();que.pop();
		int v=p.second;
		if(d[v]<p.first)continue;
		for(unsigned int i=0;i<G[v].size();i++){
		  Edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
			  d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));
			}
		}
	}
}


void G_push(int st,int to,int w){
  G[st].push_back({to,w});
  G[to].push_back({st,w});
  return;
}

int main(){
  int H,W,sx,sy,gx,gy,N;
  cin>>H>>W>>sx>>sy>>gx>>gy;
  sx--;sy--;gx--;gy--;
  N=W*H;
  V=2*N;
  vector<string> C(H);
  for(int i=0;i<H;i++)cin>>C.at(i);
  for(int i=0;i<H;i++)for(int j=0;j<W;j++){
    if(C.at(i).at(j)=='#')continue;
    if(i>0 && C.at(i-1).at(j)=='.')G_push(W*i+j,W*(i-1)+j,0);
    if(j>0 && C.at(i).at(j-1)=='.')G_push(W*i+j+N,W*i+j-1+N,0);
    G_push(W*i+j,W*i+j+N,1);
  }
  G_push(W*sx+sy,W*sx+sy+N,0);
  dijkstra(W*sx+sy);
  printf("%lld\n",min(d[W*gx+gy],d[W*gx+gy+N]));
  return 0;
}






Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var rs: int :: cui@inputInt() - 1
	var cs: int :: cui@inputInt() - 1
	var rt: int :: cui@inputInt() - 1
	var ct: int :: cui@inputInt() - 1
	
	var s: [][]char :: #[h][]char
	for y(0, h - 1)
		do s[y] :: cui@input()
	end for
	
	var dx: []int :: [1, 0, -1, 0]
	var dy: []int :: [0, 1, 0, -1]
	var qn: queue<int> :: #queue<int>
	
	const infinity: int :: lib@intMax
	var v: [][][]int :: #[h, w, 2]int
	for y(0, h - 1)
		for x(0, w - 1)
			do v[y][x][0] :: infinity
			do v[y][x][1] :: infinity
		end for
	end for
	do v[rs][cs][0] :: 0
	do v[rs][cs][1] :: 0
	for i(0, 3)
		do qn.add((rs * w + cs) * 4 + i)
	end for
	
	var cnt: int :: 0
	while(^qn <> 0)
		var q: queue<int> :: ##qn
		do qn :: #queue<int>
		while(^q <> 0)
			var val: int :: q.get()
			var pos: int :: val / 4
			var y: int :: pos / w
			var x: int :: pos % w
			var dir: int :: val % 4
			while loop(true)
				do v[y][x][dir % 2] :: [v[y][x][dir % 2], cnt].min()
				if(v[y][x][1 - dir % 2] = infinity)
					do qn.add((y * w + x) * 4 + (dir + 1) % 4)
					do qn.add((y * w + x) * 4 + (dir + 3) % 4)
				end if
				do y :+ dy[dir]
				do x :+ dx[dir]
				if(x = -1 | x = w)
					break loop
				end if
				if(y = -1 | y = h)
					break loop
				end if
				if(s[y][x] = '#')
					break loop
				end if
				if(v[y][x][dir % 2] <= cnt)
					break loop
				end if
			end while
		end while
		do cnt :+ 1
	end while
	
	var ans: int :: v[rt][ct].min()
	do cui@print("\{ans}\n")
end func
AtCoder problem 043

C++ solution:

Crystal solution: struct Point
  Direction = {Point.new(0, 1), Point.new(1, 0), Point.new(0, -1), Point.new(-1, 0)}

  property y : Int32
  property x : Int32

  def initialize(@y, @x)
  end

  def +(other : Point)
    Point.new(y + other.y, x + other.x)
  end

  def in_range?(h, w)
    (0...h).includes?(y) && (0...w).includes?(x)
  end
end

struct Vertex
  getter p : Point
  getter d : Int32

  def initialize(@p, @d)
  end

  def to_i(w)
    p.y * w * 4 + p.x * 4 + d
  end
end

h, w = read_line.split.map(&.to_i)
start, goal = {0, 1}.map {
  y, x = read_line.split.map(&.to_i.pred)
  Point.new(y, x)
}
s = (1..h).map { read_line }

dist = Array(Int32?).new(h * w * 4, nil)
deque = Deque({Vertex, Int32}).new
(0...4).each do |d|
  e = Vertex.new(start, d)
  dist[e.to_i(w)] = 0
  deque << {e, 0}
end

add_edge = ->(dist_from : Int32, to : Vertex, cost : Int32) {
  dist_to = dist[to.to_i(w)]
  if dist_to.nil? || dist_to > dist_from + cost
    dist_to = dist_from + cost
    dist[to.to_i(w)] = dist_to
    if cost == 0
      deque.push({to, dist_to})
    else
      deque.unshift({to, dist_to})
    end
  end
}

until deque.empty?
  v, dist_now = deque.pop
  dist_from = dist[v.to_i(w)].not_nil!
  next if dist_from < dist_now

  d1 = v.d
  dir = Point::Direction[v.d]
  p1, p2 = v.p, v.p + dir

  if p2.in_range?(h, w) && s[p2.y][p2.x] == '.'
    add_edge.call(dist_from, Vertex.new(p2, d1), 0)
  end
  {d1 - 1, d1 + 1}.each do |d2|
    add_edge.call(dist_from, Vertex.new(p1, d2 % 4), 1)
  end
end

puts (0...4).min_of { |d|
  dist[Vertex.new(goal, d).to_i(w)].not_nil!
}





Kuin solution: func main()
	var h: int :: cui@inputInt()
	var w: int :: cui@inputInt()
	var rs: int :: cui@inputInt() - 1
	var cs: int :: cui@inputInt() - 1
	var rt: int :: cui@inputInt() - 1
	var ct: int :: cui@inputInt() - 1
	
	var s: [][]char :: #[h][]char
	for y(0, h - 1)
		do s[y] :: cui@input()
	end for
	
	var dx: []int :: [1, 0, -1, 0]
	var dy: []int :: [0, 1, 0, -1]
	var qn: queue<int> :: #queue<int>
	
	const infinity: int :: lib@intMax
	var v: [][][]int :: #[h, w, 2]int
	for y(0, h - 1)
		for x(0, w - 1)
			do v[y][x][0] :: infinity
			do v[y][x][1] :: infinity
		end for
	end for
	do v[rs][cs][0] :: 0
	do v[rs][cs][1] :: 0
	for i(0, 3)
		do qn.add((rs * w + cs) * 4 + i)
	end for
	
	var cnt: int :: 0
	while(^qn <> 0)
		var q: queue<int> :: ##qn
		do qn :: #queue<int>
		while(^q <> 0)
			var val: int :: q.get()
			var pos: int :: val / 4
			var y: int :: pos / w
			var x: int :: pos % w
			var dir: int :: val % 4
			while loop(true)
				do v[y][x][dir % 2] :: [v[y][x][dir % 2], cnt].min()
				if(v[y][x][1 - dir % 2] = infinity)
					do qn.add((y * w + x) * 4 + (dir + 1) % 4)
					do qn.add((y * w + x) * 4 + (dir + 3) % 4)
				end if
				do y :+ dy[dir]
				do x :+ dx[dir]
				if(x = -1 | x = w)
					break loop
				end if
				if(y = -1 | y = h)
					break loop
				end if
				if(s[y][x] = '#')
					break loop
				end if
				if(v[y][x][dir % 2] <= cnt)
					break loop
				end if
			end while
		end while
		do cnt :+ 1
	end while
	
	var ans: int :: v[rt][ct].min()
	do cui@print("\{ans}\n")
end func
AtCoder problem 044

C++ solution:
#include <iostream>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
  int n,q;
  cin >> n >> q;
  deque<int> a(n);
  rep(i, n) cin >> a[i];

  rep(i, q){
    int t,x,y;
    cin >> t >> x >> y;
    x--; y--;
    if(t == 1) swap(a[x], a[y]);
    if(t == 2){
      int v = a[n - 1];
      a.erase(a.end() - 1);
      a.push_front(v);
    }
    if(t == 3) cout << a[x] << "\n";
  }
}

Crystal solution: n, q = read_line.split.map(&.to_i)
a = read_line.split.map(&.to_i)
i = 0
q.times do
  t, x, y = read_line.split.map(&.to_i.pred)
  case t
  when 0
    a[(i + x) % n], a[(i + y) % n] = a[(i + y) % n], a[(i + x) % n]
  when 1
    i -= 1
  when 2
    puts a[(i + x) % n]
  end
end