[一天至少一题直到ICPC开赛029]解题: Oil Deposits (1/19)

Oil Deposits

题目连结vj

解题

依照题目意思

连在一起的油井算一个油井
算有几个油井

递迴

找到油井==>去四周(上下左右斜边)如果有,就把它改成*==>再从他开始找(一样上下左右斜边)
直到在四周再也找不到油井,结束修改

code (java)

import java.util.*;public class Oil_Deposits {    static String[] hall;    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        while (true) {            int y = sc.nextInt();            int x = sc.nextInt();            if (x == 0 && y == 0) {                break;            }            hall = new String[y];            for (int i = 0; i < y; i++) {                hall[i] = sc.next();            }            int ans = 0;            for (int i = 0; i < y; i++) {                for (int j = 0; j < x; j++) {                    if (hall[i].charAt(j) == '@') {                        find_same(i, j);                        ans++;                    }                }            }            System.out.println(ans);        }        sc.close();    }    public static void find_same(int y, int x) {        int rows = hall.length;        int cols = hall[0].length(); // Assuming all rows have the same length        // Check below        if (y + 1 < rows && hall[y + 1].charAt(x) == '@') {            hall[y + 1] = hall[y + 1].substring(0, x) + '*' + hall[y + 1].substring(x + 1);            find_same(y + 1, x);        }        // Check above        if (y - 1 >= 0 && hall[y - 1].charAt(x) == '@') {            hall[y - 1] = hall[y - 1].substring(0, x) + '*' + hall[y - 1].substring(x + 1);            find_same(y - 1, x);        }        // Check left        if (x - 1 >= 0 && hall[y].charAt(x - 1) == '@') {            hall[y] = hall[y].substring(0, x - 1) + '*' + hall[y].substring(x);            find_same(y, x - 1);        }        // Check right        if (x + 1 < cols && hall[y].charAt(x + 1) == '@') {            hall[y] = hall[y].substring(0, x + 1) + '*' + hall[y].substring(x + 2);            find_same(y, x + 1);        }        // Check bottom right        if (x + 1 < cols && y + 1 < rows && hall[y + 1].charAt(x + 1) == '@') {            hall[y + 1] = hall[y + 1].substring(0, x + 1) + '*' + hall[y + 1].substring(x + 2);            find_same(y + 1, x + 1);        }        // Check bottom left        if (x - 1 >= 0 && y + 1 < rows && hall[y + 1].charAt(x - 1) == '@') {            hall[y + 1] = hall[y + 1].substring(0, x - 1) + '*' + hall[y + 1].substring(x);            find_same(y + 1, x - 1);        }        // Check top right        if (x + 1 < cols && y - 1 >= 0 && hall[y - 1].charAt(x + 1) == '@') {            hall[y - 1] = hall[y - 1].substring(0, x + 1) + '*' + hall[y - 1].substring(x + 2);            find_same(y - 1, x + 1);        }        // Check top left        if (x - 1 >= 0 && y - 1 >= 0 && hall[y - 1].charAt(x - 1) == '@') {            hall[y - 1] = hall[y - 1].substring(0, x - 1) + '*' + hall[y - 1].substring(x);            find_same(y - 1, x - 1);        }    }}

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章