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 ...
随机推荐
- BAPC 2014:Button Bashing(暴力+bfs)
题意: 给出n,m,代表微波炉有n个按钮,要求达到总时间为m 然后给出n个数,代表n个按钮能增加的时间,问最少几步,能够使得按出的总时间大于等于要求的时间,并且相差最小 输出最小的步数与相差的最小值 ...
- sonar 测试覆盖率
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</a ...
- 实用的vue插件大汇总
Vue是一个构建数据驱动的 web 界面的渐进式框架.Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件特别整理了常用的vue插件,来了个大汇总,方便查找使用,便于工作 ...
- pgadmin-linux-centos7.3-连接pgsql
每次远程连接linux-centos上面的pgsql的时候,需要修改ip,命令如下: -->cd /var/lib/pgsql/data -->vi pg_hba.conf 添加ip如下, ...
- Vue在单独引入js文件中使用ElementUI的组件
Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...
- 快速创建jquery插件
介绍 什么是插件? 插件我们见得很多了,比如浏览器上我们会安装一些去除广告的插件.美化页面的插件等等. 在前端,我们也常常写一些jquery插件来使用.来添加我们常常写的一些功能,方便使用. 为什么要 ...
- 数据结构之C语言模拟整数数组实现
#include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef struct Arr { in ...
- ruby逻辑判断符号
puts true and false #相当于 (puts true) and false Use &&/|| for boolean expressions, and/or fo ...
- Spring Boot集成Hibernate Validator
废话不多说,直接开始集成环境. 一.环境集成 在项目中hibernate-Validator包在spring-boot-starter-web包里面有,不需要重复引用 .(整个Demo都是用PostM ...
- 移动端适配(1)——viewport设置与初始化css
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=0 ...