webtechbharat

codeforces 45A Solution A. Codecraft lll (codeforces 45A) (rank 900*)

 45A. Codecraft lllProblem Link: https://codeforces.com/problemset/problem/45/ASolution 1:import java.util.Scanner;public class A45 {    public static Scanner scn = new Scanner(System.in);    public static String[] str = { "January", "February", "March", "April", "May", "June", "July", "August",            "September", "October", "November", "December" };    public static int nextIdx(int currIdx, int change) {        return (currIdx + change) % 12;    }    public static int currIdx(String myStr) {        for (int i = 0; i < str.length; i++) {            if (myStr.equals(str[i]))                return i;        }        return -1;    }    public static void main(String[] argv) {        String str2 = scn.next();        int change = scn.nextInt();        int idx = currIdx(str2);        System.out.println(str[nextIdx(idx, change)]);    }}

codeforces 282A Solution A. Bit++ (codeforces 282A) (rank 800*)

 282A. Bit++Problem Link: https://codeforces.com/problemset/problem/282/ASolution 1:import java.util.*;public class A282 {    public static Scanner scn = new Scanner(System.in);    public static int finalAns(int n){        String str = "";        int ans = 0;        for(int i=0;i<n;i++){            str=scn.next();            if(str.charAt(0)=='+' || str.charAt(2)=='+') ans++;            else ans--;        }        return ans;    }    public static void main(String[] argv){        int n = scn.nextInt();        System.out.println(finalAns(n));    }}

codeforces 263A Solution A. Beautiful Matrix (codeforces 263A) (rank 800*)

 263A. Beautiful MatrixProblem Link: https://codeforces.com/problemset/problem/263/ASolution 1:import java.util.*;public class A263 {    public static Scanner scn = new Scanner(System.in);    public static void input(int[][] arr) {        for (int i = 0; i < 5; i++)            for (int j = 0; j < 5; j++)                arr[i][j] = scn.nextInt();    }    public static int TotalMoves(int[][] arr) {        boolean flag = true;        int x = 0, y = 0;        for (int i = 0; i < 5; i++) {            for (int j = 0; j < 5; j++) {                if (arr[i][j] == 1) {                    x = i;                    y = j;                    flag = false;                    break;                }            }            if (!flag)                break;        }        return Math.abs(3-(x+1))+Math.abs(3-(y+1));    }    public static void main(String[] argv){        int[][] arr = new int[5][5];        input(arr);        System.out.println(TotalMoves(arr));    }}

codeforces 43D Solution D.Journey (codeforces 43D) (rank 2000*)

43D. JourneyProblem Link: "https://codeforces.com/problemset/problem/43/D"Solution 1:import java.util.*;public class traveseKing {    public static Scanner scn = new Scanner(System.in);     public static int counter = 0;     public static boolean isTelepoter(int[][] arr, int i, int j, int n, int m, int count, ArrayList<int[]> al,            int[][] dirEven, int[][] dirOdd) {        if (n == 1 && m == 1) {            System.out.println(0 + "n" + 1 + "t" + 1);            return true;        }        if (count >= n * m - 1 && ((i == 1 && j == 2) || (i == 2 && j == 1))) {            System.out.println(0 + "n" + 1 + "t" + 1);            for (int x = 0; x < al.size(); x++) {                for (int y = 0; y < 2; y++) {                    System.out.print(al.get(x)[y] + "t");                }                System.out.println();            }            System.out.println(1 + "t" + 1);             return true;        }         if (count >= n * m - 1)            counter = count + 1;        if (counter >= n * m)            return false;        boolean recAns = false;         for (int x = 0; x < dirEven.length; x++) {            int newi = 0, newj = 0;            if (m % 2 == 0) {                newi = i + dirEven[x][0];                newj = j + dirEven[x][1];            } else {                newi = i + dirOdd[x][0];                newj = j + dirOdd[x][1];            }            if ((newi <= n && newi >= 1) && (newj <= m && newj >= 1) && arr[newi - 1][newj - 1] < 1) {                al.add(new int[] { newi,newj });                arr[newi - 1][newj - 1]++;                recAns = isTelepoter(arr, newi, newj, n, m, count + 1, al, dirEven, dirOdd);                al.remove(al.size() - 1);                arr[newi - 1][newj - 1]--;            }            if (recAns)                return true;        }        return recAns;     }     public static void traverse(int[][] arr, int i, int j, int n, int m, ArrayList<int[]> al) {        boolean isTele = true;        int[][] dirOdd = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };        int[][] dirEven = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };        isTele = isTelepoter(arr, 1, 1, n, m, 0, al, dirEven, dirOdd);         if (!isTele) {            System.out.println(1);            if (n % 2 == 1)                System.out.println(n + "t" + m + "t" + 1 + "t" + 1);            else                System.out.println(n + "t" + 1 + "t" + 1 + "t" + 1);             for (int x = 0; x < n; x++) {                if (x % 2 == 0)                    for (int y = 0; y < m; y++) {                        System.out.println((x + 1) + "t" + (y + 1));                    }                else                    for (int y = m - 1; y >= 0; y--) {                        System.out.println((x + 1) + "t" + (y + 1));                     }            }            System.out.println(1 + "t" + 1);         }     }     public static void main(String[] argv) {        int n = scn.nextInt();        int m = scn.nextInt();        ArrayList<int[]> al = new ArrayList<>();        int[][] arr = new int[n][m];        traverse(arr, 1, 1, n, m, al);    }}Solution 2: Better than First:import java.util.*;public class traveseKing {    public static Scanner scn = new Scanner(System.in);    public static int counter = 0;    public static boolean isTelepoter(int[][] arr, int i, int j, int n, int m, int count, ArrayList<int[]> al,            int[][] dir) {        if (n == 1 && m == 1) {            System.out.println(0 + "n" + 1 + "t" + 1);            return true;        }        if (count >= n * m - 1 && ((i == 1 && j == 2) || (i == 2 && j == 1))) {            System.out.println(0 + "n" + 1 + "t" + 1);            for (int x = 0; x < al.size(); x++) {                for (int y = 0; y < 2; y++) {                    System.out.print(al.get(x)[y] + "t");                }                System.out.println();            }            System.out.println(1 + "t" + 1);            return true;        }        if (count >= n * m - 1)            counter = count + 1;        if (counter >= n * m)            return false;        boolean recAns = false;        for (int x = 0; x < dir.length; x++) {            int newi = 0, newj = 0;            newi = i + dir[x][0];            newj = j + dir[x][1];            if ((newi <= n && newi >= 1) && (newj <= m && newj >= 1) && arr[newi - 1][newj - 1] < 1) {                al.add(new int[] { newi, newj });                arr[newi - 1][newj - 1]++;                recAns = isTelepoter(arr, newi, newj, n, m, count + 1, al, dir);                al.remove(al.size() - 1);                arr[newi - 1][newj - 1]--;            }            if (recAns)                return true;        }        return recAns;    }    public static void traverse(int[][] arr, int i, int j, int n, int m, ArrayList<int[]> al) {        boolean istele= false;        int[][] dirOdd = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };        int[][] dirEven = { { 0, 1 }, { 1, 0 }, { -1, 0 }, { 0, -1 } };         if (m % 2 == 1 && !(n % 2 == 1 && m % 2 == 1))            istele = isTelepoter(arr, 1, 1, n, m, 0, al, dirOdd);        if(m%2==0 && !(n % 2 == 1 && m % 2 == 1))             istele = isTelepoter(arr, 1, 1, n, m, 0, al, dirEven);        if (!istele) {            System.out.println(1);            if (n % 2 == 1)                System.out.println(n + "t" + m + "t" + 1 + "t" + 1);            else                System.out.println(n + "t" + 1 + "t" + 1 + "t" + 1);            for (int x = 0; x < n; x++) {                if (x % 2 == 0)                    for (int y = 0; y < m; y++) {                        System.out.println((x + 1) + "t" + (y + 1));                    }                else                    for (int y = m - 1; y >= 0; y--) {                        System.out.println((x + 1) + "t" + (y + 1));                    }            }            System.out.println(1 + "t" + 1);        }     }    public static void main(String[] argv) {        int n = scn.nextInt();        int m = scn.nextInt();        ArrayList<int[]> al = new ArrayList<>();        int[][] arr = new int[n][m];        traverse(arr, 1, 1, n, m, al);    }}