http://acm.timus.ru/problem.aspx?space=1&num=1923 -- timus

This is s problem about thd dfs and searching like the leetcode islands problems

Creating three 2-D arrays: board, land(copy), visit.

board: the input, land:(0: undecided, 1: B country, 2: M country), visit

invading the land one by one.

for each fill, operation:

if land[i][j] = enemy return;

if land[i][j] = 0 && board[i][j] = alliance: land[i][j] = empire // 1 or 2

if land[i][j] = empire; check the neighbors of each location from board.

Heads-up: checking the boundary and visited(stack overflow)

import java.util.Scanner;

public class timus1923 {
static int visit[][] = new int[55][55];
//empire B: 1, M:2, a: original land, copy: terriority
static void fill(int x, int y, int alligance, int empire,int a[][], int copy[][],int n, int m){
if(x<0 || y<0 || x>=n || y>=m) return;//our of boundary
//check the empire board
if(visit[x][y] == 1) return;
visit[x][y] = 1;
int enemy = 0;
if(empire==1) enemy = 2;
else enemy =1; if(copy[x][y] == enemy) return;//enemy's land
if(copy[x][y]==0 && a[x][y] == alligance) copy[x][y] = empire;
if(copy[x][y]==empire) {
fill(x-1,y,alligance,empire,a,copy,n,m);
fill(x,y-1,alligance,empire,a,copy,n,m);
fill(x+1,y,alligance,empire,a,copy,n,m);
fill(x,y+1,alligance,empire,a,copy,n,m);
} }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int a[][] = new int[n+1][m+1];
int copy[][] = new int[n+1][m+1];// emprire land
//int tag[] = new int[5]; for(int i = 0; i<n; i++){
String str = in.next();
for(int j = 0; j< m; j++){
a[i][j] = str.charAt(j)-'0';
//tag[a[i][j]] ++;
}
}
int l = in.nextInt();
int b[] = new int[l+1];
for(int i = 0; i<l; i++){
b[i] = in.nextInt();
}
int countB = 0;
int countM = 0;
copy[n-1][0] = 1;
copy[0][m-1] = 2;
//start from
fill(n-1,0,a[n-1][0],1,a,copy,n,m);
fill(0,m-1,a[0][m-1],2,a,copy,n,m); for(int i = 0; i<l; i++){
//init visi
for(int j = 0; j<55; j++){
for(int k = 0; k<55; k++){
visit[j][k] = 0;
}
} if(i%2==0){
fill(n-1,0,b[i],1,a,copy,n,m);
}else{
fill(0,m-1,b[i],2,a,copy,n,m);
}
}
for(int i =0; i<n; i++){
for(int j = 0; j<m; j++){
if(copy[i][j] == 1) countB++;
else if(copy[i][j] == 2) countM++;
}
} System.out.println(countB);
System.out.println(countM);
}
}

Life is funny.

1923. Scary Politics (timus) (dfs) search的更多相关文章

  1. dfs+search

    1.数的划分 点击查看搜索 #include<iostream> #include<cstdio> #include<cmath> #include<algo ...

  2. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  3. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

  4. Leetcode: Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  5. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

  6. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  7. Trie for string LeetCode

    Trie build and search class TrieNode { public: TrieNode * next[]; bool is_word; TrieNode(bool b = fa ...

  8. 582. Kill Process

    Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...

  9. Leetcode: Closest Leaf in a Binary Tree

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

随机推荐

  1. Jury Jeopardy (这是一道单纯的模拟题)

    Description What would a programming contest be without a problem featuring an ASCII-maze? Do not de ...

  2. 启用NFS方案(读写分离)

  3. Quartz中Cron详解

    Quartz中的cron跟Linux系统的cron定义不太一样(Linux从分开始) 特殊字符: * 用来表示包含一个范围内的任意值. 例如, 分钟位置的“*” 表示 “每分钟”. ?  当不特定指代 ...

  4. 创建第一个vue工程

    vue创建项目(npm安装→初始化项目) 第一步npm安装 首先:先从nodejs.org中下载nodejs   图1 双击安装,在安装界面一直Next   图2   图3   图4 直到Finish ...

  5. mc03_IntelliJ IDEA配置github

    配置本地git仓库 首先配置一个本地的git仓库,熟悉一下git上传文件到github的过程,具体操作参考 mc02_配置本地git仓库并上传到github IntelliJ IDEA与github的 ...

  6. Android文件/文件夹选择器(支持多选操作),已封装为lib库,直接添加依赖即可。

    话不多少,先上图一览: 接下来我们开始写个app测试: 1.新建Android工程:FileSelectorTest 2.更改MainActivity: 在里面写四个textview模拟button, ...

  7. Django-5.2 模型层 多表操作

    7.3 多表操作 一.创建模型 实例:我们来假定下面这些概念,字段和关系作者模型:一个作者有姓名和年龄.作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...

  8. python 合并重叠数据

  9. Java基础16-类与对象

    1.如何创建一个类 public class Person{ //属性 String name; String genter; int age; //方法 public void eat(){ Sys ...

  10. 百度BAE数据库连接问题

    今天第一次使用百度的开发平台BAE,按照入门文档上的操作一步步来,进行的很顺利,可是我在上传了一个cms系统后,进行安装时,卡在了数据库连接这个地方,弄了一下午,终于有了结果,在这里记录起来,希望能帮 ...