1923. Scary Politics (timus) (dfs) search
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的更多相关文章
- dfs+search
1.数的划分 点击查看搜索 #include<iostream> #include<cstdio> #include<cmath> #include<algo ...
- 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 ...
- [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 ...
- Leetcode: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- PE刷题记录
PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...
- Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- Trie for string LeetCode
Trie build and search class TrieNode { public: TrieNode * next[]; bool is_word; TrieNode(bool b = fa ...
- 582. Kill Process
Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...
- 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 ...
随机推荐
- oracle中所有表的字段和注释
select t1.owner ,t1.table_name ,t1.column_id ,t1.column_name ,t1.data_type ,t2.comments from all_tab ...
- VS 2017与 Docker
https://www.cnblogs.com/rufus-hua/p/6742836.html 参考 1 基于VS2017的Docker Support体检ASP.NET Core站点的Docker ...
- mysql 死锁解决办法
查询表的时候,发现一圈圈转啊转,就是不出来数据,猜测表被锁住 解决办法 : mysql> show processlist ; mysql> kill 4; 说明 : 4为 i ...
- 案例52-crm练习新增客户中加入文件上传功能(struts2文件上传)
1 jsp/customer/add.jsp 完整代码: <%@ page language="java" contentType="text/html; char ...
- 《nginx 二》深入理解nginx的各项配置
Nginx应用场景 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机. 3. ...
- GitKraken使用教程-基础部分(9)
10. 合并分支并解决冲突(conflict) 1) 合并分支 在代码管理过程中,切换分支或者同步服务器代码时,常常会出现代码冲突的情况,这种情况出现的原因一般是由于两个分支对同一个文件进行修改, ...
- 树莓派WLAN连接PC(不通过路由器)
上一篇博文捣鼓了一下树莓派直连线连接PC.吃完午饭心血来潮,树莓派既然能用直连线连接PC,曾经也试过拿PC当作热点,为何不尝试一下用WLAN将树莓派与PC互连呢?果断搞起. 首先,我当前笔记本是用WL ...
- .Net程序员学习Linux最简单的方法(转载)
有很多关于Linux的书籍.博客.大多数都会比较“粗暴“的将一大堆的命令塞给读者,从而使很多.NET程序员望而却步.未入其门就路过了. 所以我设想用一种更为平滑的学习方式, 就是在学习命令时,先用纯语 ...
- Java中的阻塞队列-LinkedBlockingQueue(二)
原文地址:http://benjaminwhx.com/2018/05/11/%E3%80%90%E7%BB%86%E8%B0%88Java%E5%B9%B6%E5%8F%91%E3%80%91%E8 ...
- iOS实现头像选取(照相或者图片库)、大小等比缩放、生成圆形头像
//弹出actionsheet.选择获取头像的方式 //从相册获取图片 -(void)takePictureClick:(UIButton *)sender { // /*注:使用,需要实现以下协议: ...