首页 > 试题广场 >

安置路灯

[编程题]安置路灯
  • 热度指数:43876 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

小Q正在给一条长度为n的道路设计路灯安置方案。

为了让问题更简单,小Q把道路视为n个方格,需要照亮的地方用'.'表示, 不需要照亮的障碍物格子用'X'表示。

小Q现在要在道路上设置一些路灯, 对于安置在pos位置的路灯, 这盏路灯可以照亮pos - 1, pos, pos + 1这三个位置。

小Q希望能安置尽量少的路灯照亮所有'.'区域, 希望你能帮他计算一下最少需要多少盏路灯。


输入描述:
输入的第一行包含一个正整数t(1 <= t <= 1000), 表示测试用例数
接下来每两行一个测试数据, 第一行一个正整数n(1 <= n <= 1000),表示道路的长度。
第二行一个字符串s表示道路的构造,只包含'.'和'X'。


输出描述:
对于每个测试用例, 输出一个正整数表示最少需要多少盏路灯。
示例1

输入

2
3
.X.
11
...XX....XX

输出

1
3
 public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int[] len = new int[num];
        String[] path = new String[num];
        for(int i =0 ;i <num; i++){
            len[i] = sc.nextInt();
            path[i] = sc.next();
        }
        for(int i =0 ;i <num; i++){
            System.out.println(find(len[i], path[i]));
        }
    }
    public static int find(int len, String path){
        
        int res = 0;
        StringBuffer sb = new StringBuffer(path);
        
        for(int i =0; i<sb.length(); i++){
            if(sb.charAt(i) == 'X'){
                continue;
            }
            res++;
            i = i+2;
        }
        return res;
    }

发表于 2020-04-28 18:19:57 回复(0)
首先我们需要判断当前的字符串是否包含'.'(是否需要安装灯)
用str.contains(".")判断是否包含'.', 如果不包含则说明这段路不需要安装灯,所以输出结果为零,
如果包含'.',说明需要安装路灯
思路如下:
首先判断str.charAt(j)是否等于 '.' ,如果结果为真,我们需要安装的路灯数目+1,j+2;(j+2意思是直接跳到j后面的第三个路段,因为只要str.charAt(j)=='.'成立,我们只需要把路灯安装在 j+1的位置,这样能照亮j ,j+1,j+2这三个位置,所以不需要考虑j+1和j+2的情况)
代码如下
import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		
		for (int i = 0; i < n; i++) {
			int out = 0;//需要的路灯数目
			int m = cin.nextInt();
			String str = cin.next();
			if(str.contains(".")) {
				if(m<=3) {
					out=1;
					System.out.println(out);
				}
				else if(m>3) {
					for(int j=0;j<m;j++) {
						if(j==0) {
							if(str.charAt(j)=='.') {
								out++;
								j=j+2;
							}
							else continue;
						}
						else if(j==m-2) {
							if(str.charAt(j)=='.' || str.charAt(j+1)=='.') {
								out++;
								
							}
							break;
							
						}
						else if(j==m-1 ) {
							if(str.charAt(j)=='.') {
								out++;
								
							}
							break;
						}
						else {
							if(str.charAt(j)=='.') {
								out++;
								j=j+2;
							}
							else continue;
							
						}
					}
					System.out.println(out);
				}
			}
			else System.out.println(0);
		}

	}

}

发表于 2019-11-01 16:10:23 回复(1)
import java.util.Scanner;
public class Main {
    //思路:小贪心,假如第一个遇到`.`反正都要花费一个灯来照亮,所以就把这个灯安装到下一个位置
    //然后来到第4个格子,同时也说明新来到的格子不能依靠前面的灯来把自己照亮
    public static int light(String road) {
        if(road == null || road.length() == 0) {
            return 0;
        }
        int n = road.length();
        int ans = 0;
        //for循环中的变量i:
        //若遇到'.'则向后移3位,
        //若遇到'X'则向后移1位
        for(int i = 0; i < n; i++) {
            if(road.charAt(i) == '.') {
                ans++;
                i += 2;
            }
        }

        return ans;
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = Integer.valueOf(s.nextLine());
        for(int i = 0; i < n; i++) {
            s.nextLine();
            System.out.println(light(s.nextLine()));
        }
    }
}
发表于 2019-09-22 10:05:17 回复(0)
import java.util.Scanner;


public class Main{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        int n[] = new int[t];
        String s[] = new String[t];
        for(int i = 0; i < t; i++) {
        	n[i] = input.nextInt();
        	s[i] = input.next();
        }
        input.close();
        for(int i = 0; i < t; i++) {
        	System.out.println(Solution(s[i]));
        }
    }
    public static int Solution(String s) {
		int n = s.length();
    	int k = 0;
		boolean f = false;
		for(int i = 0; i < n; i++) {
			if(s.charAt(i) == '.') {
				f = true;
				k = i;
				break;
			}
		}
		if(!f)//全是障碍物,不需要路灯
			return 0;
		else if( k+3 >= n)//需要照亮的位置在倒数后三个格子里,需要一个路灯
			return 1;
		else//不在后三个格子里,后移3个位置递归计算
			return 1 + Solution(s.substring(k+3, n));  	
    }
}

编辑于 2019-09-04 14:46:38 回复(0)
import java.util.*;
//贪心算法求解:1.当遇到第一个'.'时,表示该位置需要被照亮,此时不安装路灯,在它的下一个位置安装路灯,即sum+1;
//因为该路灯位置的下一个位置已经被照亮了,因此下标+2
//遇到‘X’时跳过,因为不需要安装
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for(int i = 0; i < t; i++){
            int n = sc.nextInt();
            String s = sc.next();
            int sum = 0;
            for(int j = 0; j < n; j++){
                if(s.charAt(j) == '.'){
                    sum++;
                    j = j + 2;
                }
            }
            System.out.println(sum);
        }
    }
}

发表于 2019-09-02 15:20:07 回复(1)
import java.util.Scanner;
public class LayLamp {
     //用的是贪心算法, 假设当前位置之前都已经照亮了
     //碰到“.”就往后+2,再加上循环里的++,相当于+3了
     //因为在此位置之前都已经照亮了,当前位置摆放的灯要尽可能照亮多的位置,一个灯能照三格位   置,所以加三
     //遇到"X",不放置,循环体自动+1,跳过该位置
     public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);
          int n = sc.nextInt();
          for(int i =0; i < n; i++){
               int len = sc.nextInt();
               String line = sc.next();
               int count = 0;
               for(int j=0; j < len; j++){
                    char c = line.charAt(j);
                    if(c == '.'){
                         j += 2;
                         count++;
                    }
               }
              System.out.println(count);
          }
     }
}

发表于 2019-08-12 21:43:02 回复(0)
可以用贪心的思想,推导过程如下,从前往后遍历,遇上'.'则放置路灯。
  1. 遇上了'.',如果在当前'.'放置路灯,那么能照到当前和下一个(因为我们是从头开始遍历,在遍历到这个'.'之前都已经被照亮了)。如果在下一个位置放灯,那么能照亮当前位置和后两个,所以一定是在下一个位置放灯照亮的最多。
  2. 最后考虑一下遍历到末尾的边界情况。
  3. 遇上‘X’不考虑
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t > 0) {
            t--;
            int n = sc.nextInt();
            String s = sc.next();
            int sum = 0;
            char[] c = s.toCharArray();
            for (int i = 0; i < n; i++) {
                if (c[i] == '.') {
                    if (i + 2 < n) {
                        c[i] = 'X';
                        c[i + 1] = 'X';
                        c[i + 2] = 'X';
                        sum++;
                    } else if (i + 1 == n - 1) {
                        c[i] = 'X';
                        c[i + 1] = 'X';
                        sum++;
                    } else if (i == n - 1) {
                        c[i] = 'X';
                        sum++;
                    }
                }
            }
            System.out.println(sum);
        }

    }
}


发表于 2019-08-11 11:54:34 回复(0)
/**
贪心:遇到一个·就直接跳三步,因为中间放一个路灯就能覆盖到这三个,无论这三个是.还是X。遇到X则忽略继续往后走。
*/
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in =new Scanner(System.in);
        int nums = in.nextInt();
        while(nums-->0){
            int len = in.nextInt();
            char[] road = new char[len];
            String str= in.next();
            road=str.toCharArray();
            System.out.println(deployNum(road,len));
        }
    }

    public static int deployNum(char[] road,int len) {
        int result = 0;
        for(int i = 0;i<len;i++){
            if(road[i]=='.'){
                i+=2;
                result++;
            }
        }
        return result;
    }
}

发表于 2019-08-10 22:39:14 回复(0)
Java解法:找到规律,碰到第一个‘.’就往后走两位。
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int t = input.nextInt();
        for (int i=0;i<t;i++){
            int num = input.nextInt();
            String s = input.next();
            int count = 0;
            for (int j=0;j<num;j++){
                if (s.charAt(j)=='.'){
                    count++;
                    j = j+2;
                }
            }
            System.out.println(count);
        }
    }
}

发表于 2019-08-04 19:58:23 回复(0)
import java.util.Scanner;

public class Main {
    /**
     * 解决方案:在.的下一个路灯上安置,然后跳过一个,再找下一个.
     */

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine().toString();
        int testNum = Integer.parseInt(string);

        int curTestNum=0;
        while (curTestNum < testNum) {
            String str = scanner.nextLine().toString();
            int lightNum = Integer.parseInt(str);//路灯个数
            String lightStr = scanner.nextLine().toString();
            int index=0;
            int count=0;
            while (index<lightStr.length()){
                if(lightStr.charAt(index)=='.'){
                    count++;
                    index=index+3;
                    continue;
                }
                index++;
            }
            System.out.println(count);

            curTestNum++;
        }

    }
}

发表于 2018-05-07 10:18:47 回复(0)

热门推荐

通过挑战的用户

查看代码