zoj2412
来源:互联网 发布:淘宝买家数据采集 编辑:程序博客网 时间:2024/06/05 20:23
import java.io.BufferedInputStream;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Scanner;public class Main{ public static void main(String[] args) throws Exception{ Scanner cin = new Scanner(new BufferedInputStream(System.in)) ; PrintWriter cout = new PrintWriter(System.out); while(cin.hasNext()){ int n = cin.nextInt() , m = cin.nextInt() ; if(n == -1 && m == -1) break ; new Task().solve(n, m , cin , cout) ; } cout.flush() ; } }class Task{ static class Node{ public boolean left , right , up , down ; public Node(boolean u , boolean d , boolean l ,boolean r){ up = u ; down = d ; left = l ; right = r ; } public boolean cango(int d , Node o){ switch(d){case 0:if(up && o.down) return true ; break;case 1:if(down && o.up) return true ; break;case 2:if(left && o.right) return true ; break;case 3:if(right && o.left) return true ; break;default: break;} return false ; } } HashMap<Character , Node> map = new HashMap<Character, Task.Node>() ; { map.put('A', new Node(true, false ,true , false)) ; map.put('B', new Node(true, false, false, true)) ; map.put('C', new Node(false, true ,true ,false )) ; map.put('D', new Node(false, true ,false ,true )) ; map.put('E', new Node(true, true ,false ,false )) ; map.put('F', new Node(false, false ,true ,true )) ; map.put('G', new Node(true, false,true ,true )) ; map.put('H', new Node(true, true, true , false)) ; map.put('I', new Node(false, true ,true ,true )) ; map.put('J', new Node(true, true, false ,true )) ; map.put('K', new Node(true, true, true ,true )) ; } char[][] str ; boolean[][] vis ; int[][] dir = new int[][]{{-1,0}, {1,0} , {0,-1} ,{0,1}} ; int n , m ; public boolean can(int x , int y){ return 0 <= x && x < n && 0 <= y && y < m ; } public void solve(int n , int m , Scanner cin , PrintWriter cout){ this.n = n ; this.m = m ; str = new char[n][m] ; for(int i = 0 ; i < n ; i++) str[i] = cin.next().toCharArray() ; vis = new boolean[n][m] ; int sum = 0 ; for(int i = 0 ; i < n ; i++){ for(int j = 0 ; j < m ; j++){ if(! vis[i][j]){ sum++ ; dfs(i , j) ; } } } cout.println(sum) ; // cout.flush() ; } public void dfs(int x , int y){ vis[x][y] = true ; for(int i = 0 ; i < dir.length ; i++){ int nx = x + dir[i][0] ; int ny = y + dir[i][1] ; if(can(nx , ny) && ! vis[nx][ny] && map.get(str[x][y]).cango(i , map.get(str[nx][ny]))) dfs(nx, ny) ; } }}
0 0
- zoj2412
- zoj2412题解
- zoj2412 hdu1198
- zoj2412:Farm Irrigation
- zoj2412-Farm Irrigation
- zoj2412 dfs连通图
- 农场灌溉问题ZOJ2412
- ZOJ2412 Farm Irrigation (DFS)
- zoj2412 Farm Irrigation 搜索dfs
- zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
- “农田灌溉(Farm Irrigation), ZOJ2412”问题的一种解法
- Linux基本命令1
- IOS--C 语言 Lesson Eight Struct
- Stanford机器学习---第十一讲.异常检测
- iOS UILabel
- shopnc b2b2c初次运行提示Class Error: mysqli.isn't exists!的解决方法
- zoj2412
- 黑马程序员---2015.6.26java基础笔记--File类--Properties--PrintStream--SequenceInputStream--RandomAccessFile
- WebView控件应用——模仿URL有道词典
- 为什么使用portlet?
- 金士顿KTD-INSP6000C/2G内存
- iOS-学习笔记-UI-第十九天
- 在Android APK中调用底层linux命令或者脚本
- JS Library Set
- springLdap 操作ldap示例(增删改查)