// Topcoder SRM 576 DIV 2 L2 ArcadeManao
import java.util.*;
import java.math.*;
//rename the class name before submitting
public class ArcadeManao {
public static void main(String[] args) {
ArcadeManao obj = new ArcadeManao();
Object o =
obj.shortestLadder(
null,
0,
0
);
System.out.println(o);
}
static boolean found;
public int shortestLadder(String[] level, int coinRow, int coinColumn) {
int row = level.length;
int col = level[0].length();
int L;
for (L = 0; L < row; L++) {
boolean[][] visited = new boolean[row][col];
found = false;
Search(L, level, row, 1, coinRow, coinColumn, visited);
if (found)
break;
}
return L;
}
private void Search(int L, String[] lev, int fromRow, int fromCol, int coinR,
int coinC, boolean[][] visited) {
if (found)
return;
if (fromRow < 1 || fromRow > lev.length || fromCol < 1
|| fromCol > lev[0].length())
return;
if (visited[fromRow - 1][fromCol - 1])
return;
else
visited[fromRow - 1][fromCol - 1] = true;
if (lev[fromRow - 1].charAt(fromCol - 1) == '.')
return;
if (fromRow == coinR && fromCol == coinC)
found = true;
// to left
if (fromCol > 1 && lev[fromRow - 1].charAt(fromCol - 1 - 1) == 'X')
Search(L, lev, fromRow, fromCol - 1, coinR, coinC, visited);
// to right
if (fromCol < lev[0].length()
&& lev[fromRow - 1].charAt(fromCol - 1 + 1) == 'X')
Search(L, lev, fromRow, fromCol + 1, coinR, coinC, visited);
for (int LL = 1; LL <= L; LL++) {
// up
int newR = fromRow - LL;
Search(L, lev, newR, fromCol, coinR, coinC, visited);
// down
newR = fromRow + LL;
Search(L, lev, newR, fromCol, coinR, coinC, visited);
}
return;
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Showing posts with label dfs. Show all posts
Showing posts with label dfs. Show all posts
Sunday, June 2, 2013
Wednesday, March 27, 2013
Topcoder SRM 574 DIV 2 L2 TheNumberGameDiv2
//Topcoder SRM 574 DIV 2 L2 TheNumberGameDiv2
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TheNumberGameDiv2 {
public static void main(String[] args) {
System.out.println(
//
new TheNumberGameDiv2().minimumMoves(
218181918,
9181
));
}
HashSet<Integer> nums = new HashSet<Integer>();
int minmov;
public int minimumMoves(int A, int B) {
if (A == B)
return 0;
minmov = Integer.MAX_VALUE;
dfs(A, B, 0);
if (minmov == Integer.MAX_VALUE)
return -1;
else
return minmov;
}
private void dfs(int a, int b, int mov) {
if (a == b) {
minmov = Math.min(minmov, mov);
return;
}
if (a == 0)
return;
nums.add(a);
String rev = new StringBuffer(String.valueOf(a)).reverse().toString();
int arev = Integer.valueOf(rev);
if (!nums.contains(arev)) {
dfs(arev, b, mov + 1);
}
dfs(a / 10, b, mov + 1);
}
}
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TheNumberGameDiv2 {
public static void main(String[] args) {
System.out.println(
//
new TheNumberGameDiv2().minimumMoves(
218181918,
9181
));
}
HashSet<Integer> nums = new HashSet<Integer>();
int minmov;
public int minimumMoves(int A, int B) {
if (A == B)
return 0;
minmov = Integer.MAX_VALUE;
dfs(A, B, 0);
if (minmov == Integer.MAX_VALUE)
return -1;
else
return minmov;
}
private void dfs(int a, int b, int mov) {
if (a == b) {
minmov = Math.min(minmov, mov);
return;
}
if (a == 0)
return;
nums.add(a);
String rev = new StringBuffer(String.valueOf(a)).reverse().toString();
int arev = Integer.valueOf(rev);
if (!nums.contains(arev)) {
dfs(arev, b, mov + 1);
}
dfs(a / 10, b, mov + 1);
}
}
Sunday, December 16, 2012
Codeforces Round #150 (Div. 2): B. Undoubtedly Lucky Numbers
// Codeforces Round #150 (Div. 2): B. Undoubtedly Lucky Numbers
import java.io.*;
import java.math.*;
import java.util.*;
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}
solve();
}
private static void solve() throws IOException
{
int N = in.nextInt();
cnt = 0;
dfs(0, N);
out.print(cnt);
}
static int cnt;
static Set<Integer> set = new HashSet<Integer>();
private static void dfs(int x, int N) {
if (x > N)
return;
for (int i = 0; i <= 9; i++) {
int xx = x * 10 + i;
if (xx > 0 && xx <= N) {
int test = xx;
set.clear();
while (test > 0) {
set.add(test % 10);
test = test / 10;
if (set.size() > 2)
break;
}
if (set.size() <= 2) {
cnt++;
dfs(xx, N);
}
}
}
}
static class MyScanner {
StreamTokenizer in;
public MyScanner() throws IOException
{
Reader r = new BufferedReader(new InputStreamReader(System.in));
in = new StreamTokenizer(r);
}
public MyScanner(String inputFile) throws IOException {
Reader r;
r = new BufferedReader(new FileReader(inputFile));
in = new StreamTokenizer(r);
}
public int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public long nextLong() throws IOException {
in.nextToken();
return (long) in.nval;
}
public double nextDouble() throws IOException {
in.nextToken();
return in.nval;
}
public String nextString() throws IOException {
in.nextToken();
return in.sval;
}
}
}
import java.io.*;
import java.math.*;
import java.util.*;
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;
// LOCAL_TEST = true;// comment it before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}
solve();
}
private static void solve() throws IOException
{
int N = in.nextInt();
cnt = 0;
dfs(0, N);
out.print(cnt);
}
static int cnt;
static Set<Integer> set = new HashSet<Integer>();
private static void dfs(int x, int N) {
if (x > N)
return;
for (int i = 0; i <= 9; i++) {
int xx = x * 10 + i;
if (xx > 0 && xx <= N) {
int test = xx;
set.clear();
while (test > 0) {
set.add(test % 10);
test = test / 10;
if (set.size() > 2)
break;
}
if (set.size() <= 2) {
cnt++;
dfs(xx, N);
}
}
}
}
static class MyScanner {
StreamTokenizer in;
public MyScanner() throws IOException
{
Reader r = new BufferedReader(new InputStreamReader(System.in));
in = new StreamTokenizer(r);
}
public MyScanner(String inputFile) throws IOException {
Reader r;
r = new BufferedReader(new FileReader(inputFile));
in = new StreamTokenizer(r);
}
public int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
public long nextLong() throws IOException {
in.nextToken();
return (long) in.nval;
}
public double nextDouble() throws IOException {
in.nextToken();
return in.nval;
}
public String nextString() throws IOException {
in.nextToken();
return in.sval;
}
}
}
Subscribe to:
Posts (Atom)