import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class MST {
public static void kruskal(int []V,Edge[] E) {
Arrays.sort(E);
ArrayList<HashSet> sets=new ArrayList<HashSet>();
for(int i=0;i<V.length;i++) {
HashSet set=new HashSet();
set.add(V[i]);
sets.add(set);
}
int sum_w=0;
for(int i=0;i<E.length;i++) {
int start=E[i].i;
int end=E[i].j;
int w=E[i].w;
int set_start=-1;int set_end=-2;
for(int j=0;j<sets.size();j++) {
HashSet set=sets.get(j);
if(set.contains(start))
set_start=j;
if(set.contains(end))
set_end=j;
}
if(set_start!=set_end) {
sum_w+=E[i].w;
HashSet set=sets.get(set_end);
sets.remove(set_end);
HashSet set1=sets.get(set_start);
sets.remove(set_start);
set1.addAll(set);
sets.add(set1);
} }
System.out.println(sum_w); } public static void main(String[] args) {
// TODO Auto-generated method stub
int []V= {1,2,3,4,5,6};
Edge[]E=new Edge[10];
E[0]=new Edge(1,2,6);
E[1]=new Edge(1,3,1);
E[2]=new Edge(1,4,5);
E[3]=new Edge(2,3,5);
E[4]=new Edge(2,5,3);
E[5]=new Edge(3,4,5);
E[6]=new Edge(3,5,6);
E[7]=new Edge(3,6,4);
E[8]=new Edge(4,6,2);
E[9]=new Edge(5,6,6);
kruskal(V,E);
} public static class Edge implements Comparable{
public int i,j,w;
public Edge(int i,int j,int w) {
this.i=i;
this.j=j;
this.w=w;
}
@Override
public int compareTo(Object o) {
Edge to=(Edge) o;
if(this.w>to.w)return 1;
else if(this.w==to.w)return 0;
else return -1;
}
@Override
public String toString() {
return "start="+i+"||end="+j+"||weight="+w;
} }
}
public class ShortestPath {
public static void shortestPath(int graph[][], int start, int n) {
int[] d = new int[n];
int pre[] = new int[n];
boolean[] finish = new boolean[n];
int noEdge = Integer.MAX_VALUE;
int noPre = -1;
int noLink = 0;
// 初始化
for (int i = 0; i < n; i++) {
if (graph[start][i] == noLink) {
d[i] = noEdge;
pre[i] = noPre;
} else {
d[i] = graph[start][i];
pre[i] = start;
}
finish[i] = false;
} finish[start] = true;
pre[start] = noPre;
d[start] = noEdge;
for (int i = 0; i < n; i++) {
int minj = -1, minValue = Integer.MAX_VALUE;
// 找最小的d值
for (int j = 0; j < n; j++) {
if (!finish[j]) {
if (d[j] < minValue) {
minj = j;
minValue = d[j];
}
}
}
if (minj != -1) {
int count = minj;
// 输出最短路径
System.out.print(minj + 1);
while (pre[count] != noPre) {
System.out.print(" " + (pre[count] + 1));
count = pre[count];
}
System.out.println();
} else {
// System.out.println("不再存在从v0可到达的最短路径");
break;
}
// 更新d pre finish
finish[minj] = true;
for (int j = 0; j < n; j++) {
if (!finish[j]) {
if (graph[minj][j] != noLink && (d[minj] + graph[minj][j] < d[j])) {
pre[j] = minj;
d[j] = d[minj] + graph[minj][j];
}
}
}
} }
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] graph = { { 0, 2, 4, 3 }, { 2, 0, 2, 0 }, { 4, 2, 0, 1 }, { 3, 0, 1, 0 } }; shortestPath(graph, 0, 4);
}
}

ccf201709

0901打酱油(100分)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
Scanner scan=new Scanner(System.in); int money=scan.nextInt();
scan.close();
int num_buy=0;
if (money%50==0)
{
num_buy=money/50*7;
}
else {
num_buy=money%50/30+money%50/10+money/50*7;
}
System.out.println(num_buy); } }

0902公共钥匙盒(100分)

import java.util.Arrays;
import java.util.Scanner; public class Main_02 { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt(); int[] key = new int[n];
for (int i = 0; i < n; i++) {
key[i] = i + 1;
} int[][] tea_use = new int[k][4];
for (int i = 0; i < k; i++) {
tea_use[i][0] = scan.nextInt();
tea_use[i][1] = scan.nextInt();
tea_use[i][2] = scan.nextInt();
tea_use[i][3] = tea_use[i][1] + tea_use[i][2];
} int time = 0;
int max = 0;
for (int i = 0; i < k; i++) {
if (tea_use[i][3] > max) {
max = tea_use[i][3];
}
} int[] ret = new int[k];
while (time <= max) {
for (int i = 0; i < k; i++) {
ret[i] = 0;
}
int m = 0;
for (int i = 0; i < k; i++) {
if (tea_use[i][3] == time) {
ret[m++] = tea_use[i][0];
}
}
Arrays.sort(ret);
for (int i = 0; i < k; i++) {
if (ret[i] != 0) {
for (int j = 0; j < n; j++) {
if (key[j] == 0) {
key[j] = ret[i];
ret[i] = 0;
break;
}
}
}
} for (int i = 0; i < k; i++) {
if (tea_use[i][1] == time) {
for (int j = 0; j < n; j++) {
if (key[j] == tea_use[i][0]) {
key[j] = 0;
}
}
}
}
time++;
} for (int i = 0; i < n; i++) {
System.out.print(key[i] + " ");
} } }

0903Json查询()

借鉴 CCF 201709-3 Json查询 Java

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; public class Main_03 {
static Map<String,String> Json=new HashMap<String,String>();
static String keyVal="";
static boolean key=false; public static void handle(String line) {
for(int i=0;i<line.length();i++) {
char c=line.charAt(i);
switch(c) {
case '{':
Json.put(keyVal, "OBJECT");
key=true;
break; case '}':
if(!keyVal.equals("")) {
int j;
for(j=keyVal.length()-1;j>=0;j--) {
if(keyVal.charAt(j)=='.') {
break;
}
}
if(j<0)
keyVal="";
else
keyVal=keyVal.substring(0,j);
}
break; case '"':
String temp="";
for(i=i+1;i<line.length();i++) {
if(line.charAt(i)=='\\') {
i++;
temp+=line.charAt(i);
}
else if(line.charAt(i)=='"')
break;
else
temp+=line.charAt(i);
}
if(key) {
if(!keyVal.equals(""))
keyVal+=".";
keyVal+=temp;
}
else {
Json.put(new String(keyVal), "STRING "+temp);
int j=keyVal.lastIndexOf(".");
if(j<0)
keyVal="";
else
keyVal=keyVal.substring(0,j);
}
break; case ':':
key=false;
break; case ',':
key=true;
break;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
scan.nextLine(); for(int i=0;i<n;i++) {
handle(scan.nextLine().replace(" ", ""));
} for(int i=0;i<m;i++) {
String query=scan.nextLine();
if(!Json.containsKey(query))
System.out.println("NOTEXIST");
else
System.out.println(Json.get(query));
} }
}

0904通信网络(100分)

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner; public class Main_04 { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
List[] send = new ArrayList[n + 1];
List[] receive = new ArrayList[n + 1];
for (int i = 0; i < n + 1; i++) {
send[i] = new ArrayList();
receive[i] = new ArrayList();
} for (int j = 0; j < m; j++) {
int s = scan.nextInt();
int t = scan.nextInt();
send[s].add(t);
receive[t].add(s);
} boolean all[][] = new boolean[n + 1][n + 1];
for (int i = 0; i < n + 1; i++) {
all[i][i] = true;
} Queue q = new LinkedList();
boolean[] flags;
for (int i = 1; i < n + 1; i++) {
q.add(i);
flags = new boolean[n + 1];
while (!q.isEmpty()) {
int temp = (int) q.poll();
if (!flags[temp]) {
for (int j = 0; j < send[temp].size(); j++) {
int next = (int) send[temp].get(j);
q.add(next);
}
flags[temp] = true;
all[i][temp] = true;
}
}
} for (int i = 1; i < n + 1; i++) {
q.add(i);
flags = new boolean[n + 1];
while (!q.isEmpty()) {
int temp = (int) q.poll();
if (!flags[temp]) {
for (int j = 0; j < receive[temp].size(); j++) {
int next = (int) receive[temp].get(j);
q.add(next);
}
flags[temp] = true;
all[i][temp] = true;
}
}
}
int num = 0;
for (int i = 1; i < n + 1; i++) {
boolean knowAll = true;
for (int j = 1; j < n + 1; j++) {
if (!all[i][j]) {
knowAll = false;
break;
}
}
if (knowAll) {
num++;
}
} System.out.println(num); } }

0905除法(30分)

import java.util.Scanner;

public class Main_05 {

    public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt(); int input1[] = new int[n + 1];
for (int i = 1; i < n + 1; i++) {
input1[i] = scan.nextInt();
}
int result[] = new int[m];
int k = 0;
for (int i = 0; i < m; i++) {
int opt = scan.nextInt();
int l = scan.nextInt();
int r = scan.nextInt();
if (opt == 1) {
int v = scan.nextInt();
for (int j = l; j < r + 1; j++) {
if (input1[j] % v == 0) {
input1[j] /= v;
}
}
}
if (opt == 2) {
int sum = 0;
for (int j = l; j < r + 1; j++) {
sum += input1[j];
}
System.out.println(sum);
} }
} }

ccf201703

0301分蛋糕(100分)

package test_201703;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int k=scan.nextInt();
int weights[]=new int[n];
for(int i=0;i<n;i++) {
weights[i]=scan.nextInt();
}
int people=0;
int sum=0;
for(int i=0;i<n;i++) {
sum+=weights[i];
if(sum>=k||i==n-1) {
people++;
sum=0;
}
}
System.out.println(people);
}
}

0302学生排队(100分)

package test_201703;

import java.util.Scanner;

public class Main_02 {

    public static void main(String[] args) {
// TODO Auto-generated method stub Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int opt_num=scan.nextInt(); int student[]=new int[n+1];
int pos[]=new int[n+1];
for(int i=1;i<n+1;i++) {
student[i]=i;
pos[i]=i;
} for(int i=0;i<opt_num;i++) {
int stu=scan.nextInt();
int dis=scan.nextInt();
//向后移
if(dis>0) {
int temp=student[pos[stu]];
int j=0;
for(j=pos[stu];j<pos[stu]+dis;j++) {
student[j]=student[j+1];
pos[student[j+1]]-=1;
}
pos[stu]+=dis;
student[j]=temp;
}
//向前移
else {
int temp=student[pos[stu]];
int j=0;
for(j=pos[stu];j>pos[stu]+dis;j--) {
student[j]=student[j-1];
pos[student[j-1]]+=1;
}
pos[stu]+=dis;
student[j]=temp; }
} for(int i=1;i<n+1;i++) {
System.out.print(student[i]+" ");
}
} }

0304地铁修建(80分)运行超时。借鉴http://blog.csdn.net/eternity666/article/details/68974954

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner; public class Main_04 {
static int maxN = 100001;
static int maxValue = Integer.MAX_VALUE;
static int costo[] = new int[maxN];
static ArrayList<Edge>[] G = new ArrayList[maxN];
static boolean vis[] = new boolean[maxN]; public static void shortestPath(int start, int n) {
PriorityQueue<Node> pq = new PriorityQueue<Node>();
for (int i = 0; i <= n; i++) {
costo[i] = maxValue;
vis[i] = false;
}
vis[0] = true;
costo[start] = 0;
pq.add(new Node(start, 0));
Node temp;
while (!pq.isEmpty()) {
temp = pq.poll();
int v = temp.v;
if (vis[v])
continue;
vis[v] = true;
for (int i = 0; i < G[v].size(); i++) {
int target = G[v].get(i).target;
int cost = G[v].get(i).cost;
int maxCost = Math.max(cost, costo[v]);
if (!vis[target] && costo[target] > maxCost) {
costo[target] = maxCost;
pq.add(new Node(target, costo[target]));
}
}
}
System.out.println(costo[n]);
} public static void main(String[] args) {
// TODO Auto-generated method stub for (int i = 0; i < maxN; i++) {
G[i] = new ArrayList<Edge>();
}
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt(); for (int i = 0; i < m; i++) {
int a = s.nextInt();
int b = s.nextInt();
int c = s.nextInt();
G[a].add(new Edge(b, c));
G[b].add(new Edge(a, c));
} shortestPath(1, n); } public static class Node implements Comparable {
public int v, dis; public Node(int v, int dis) {
this.v = v;
this.dis = dis;
} @Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
Node n = (Node) o;
if (this.dis > n.dis)
return 1;
else if (this.dis == n.dis)
return 0;
else
return -1;
} } public static class Edge {
public int target, cost; public Edge(int target, int cost) {
this.target = target;
this.cost = cost;
} }
}

也可借鉴https://www.cnblogs.com/freinds/p/6742618.html

ccf201612

1201中间数(100分)

package ccf_201612;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
List l=new ArrayList();
for(int i=0;i<n;i++) {
l.add(s.nextInt());
}
Collections.sort(l); if(n==1) {
System.out.println(l.get(0));
return;
} if(l.size()%2==0) {
int mid=(int) l.size()/2-1;
int mid_num=(int) l.get(mid);
int num=0;
for(int j=0;j<l.size();j++) {
if((int)l.get(j)==mid_num)
num++;
}
if(num%2==0) {
System.out.println(l.get(mid));
}
else
System.out.println(-1);
} if(l.size()%2==1) {
int mid=(int) (l.size()+1)/2-1;
int mid_num=(int) l.get(mid);
int num=0;
for(int j=0;j<l.size();j++) {
if((int)l.get(j)==mid_num)
num++;
}
if(num%2==1) {
System.out.println(l.get(mid));
}
else
System.out.println(-1);
} } }

1202工资计算(100分)

package ccf_201612;

import java.util.Scanner;

public class Main_02 {

    public static int algoth(int all) {
int after=0;
if(all<=3500) {
after=all;
return after;
}
all-=3500;
if(all<=1500) {
after= (int) (all*0.97);
}
if(1500<all&&all<=4500) {
after=(int) (all-(all-1500)*0.1-1500*0.03);
}
if(4500<all&&all<=9000) {
after=(int) (all-(all-4500)*0.2-3000*0.1-1500*0.03);
}
if(9000<all&&all<=35000) {
after=(int) (all-(all-9000)*0.25-4500*0.2-3000*0.1-1500*0.03);
}
if(35000<all&&all<=55000) {
after=(int) (all-(all-35000)*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03);
}
if(55000<all&&all<=80000) {
after=(int) (all-(all-55000)*0.35-20000*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03);
}
if(all>80000) {
after=(int) (all-(all-80000)*0.45-25000*0.35-20000*0.3-26000*0.25-4500*0.2-3000*0.1-1500*0.03);
}
return after+3500; } public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int money=s.nextInt(); int after[]=new int[100001];
for(int i=0;i<100001;i++) {
after[i]=0;
} for(int i=1;i<=100000;i++) { int aft=algoth(i); if(i%100==0) {
after[aft]=i;
} } System.out.println(after[money]); } }

ccf201609

0901最大波动(100分)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt(); List l=new ArrayList();
int input[]=new int[n];
int first=s.nextInt();
for(int i=0;i<n-1;i++) {
int second=s.nextInt();
int abs=Math.abs(first-second);
l.add(abs);
first=second;
}
Collections.sort(l);
int max=(int) l.get(l.size()-1);
System.out.println(max);
} }

0902火车购票(90分)

90分

import java.util.Scanner;

public class Main_02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt(); int seat[][] = new int[20][5];
int remain[] = new int[20];
for (int i = 0; i < 20; i++) {
seat[i][0] = i * 5 + 1;
seat[i][1] = i * 5 + 2;
seat[i][2] = i * 5 + 3;
seat[i][3] = i * 5 + 4;
seat[i][4] = i * 5 + 5;
}
for (int i = 0; i < 20; i++) {
remain[i] = 5;
} int ticket[] = new int[n];
for (int i = 0; i < n; i++) {
ticket[i] = s.nextInt();
} for (int i = 0; i < n; i++) {
int num = ticket[i];
boolean flag = false; for (int j = 0; j < 20; j++) {
if (remain[j] >= num) {
for (int k = 0; k < num; k++) {
System.out.print(seat[j][5 - remain[j]] + " ");
remain[j] -= 1;
}
System.out.println();
flag = true;
break;
}
} if (!flag) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k < remain[j]; k++) {
System.out.print(seat[j][5 - remain[j]] + " ");
remain[j] -= 1;
num--;
}
if (num == 0) {
System.out.println();
break;
}
} } } } }

测试用例如下时出错:

输入:

21
4 4 3 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
4 输出: 1 2 3 4
6 7 8 9
11 12 13
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
41 42 43 44 45
46 47 48 49 50
51 52 53 54 55
56 57 58 59 60
61 62 63 64 65
66 67 68 69 70
71 72 73 74 75
76 77 78 79 80
81 82 83 84 85
86 87 88 89 90
91 92 93 94 95
96 97 98 99 100
5 10 14 最后一行应为5 10 14 15

出错代码:

for (int k = 0; k < remain[j]; k++) {
System.out.print(seat[j][5 - remain[j]] + " ");
//此处改变了remain[j]
remain[j] -= 1;
num--;
if (num == 0) {
System.out.println();
flag=true;
break;
}
}

java满分代码:

import java.util.Scanner;

public class Main_02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt(); int seat[][] = new int[20][5];
int remain[] = new int[20];
for (int i = 0; i < 20; i++) {
seat[i][0] = i * 5 + 1;
seat[i][1] = i * 5 + 2;
seat[i][2] = i * 5 + 3;
seat[i][3] = i * 5 + 4;
seat[i][4] = i * 5 + 5;
}
for (int i = 0; i < 20; i++) {
remain[i] = 5;
} int ticket[] = new int[n];
for (int i = 0; i < n; i++) {
ticket[i] = s.nextInt();
} for (int i = 0; i < n; i++) {
int num = ticket[i];
boolean flag = false; for (int j = 0; j < 20; j++) {
if (remain[j] >= num) {
for (int k = 0; k < num; k++) {
System.out.print(seat[j][5 - remain[j]] + " ");
remain[j] -= 1;
}
System.out.println();
flag = true;
break;
}
} if (!flag) {
for (int j = 0; j < 20; j++) {
int r=remain[j];
for (int k = 0; k < r; k++) {
System.out.print(seat[j][5 - remain[j]] + " ");
remain[j] -= 1;
num--;
if (num == 0) {
System.out.println();
flag=true;
break;
}
}
if(flag)
break; } } } } }

0904:交通规划 (100分)  借鉴 ccf交通规划 利用dijkstra和优先队列 c++

(借鉴  java

Dijkstra求解单源点最短路径

http://blog.csdn.net/moilk_nepho/article/details/52950546

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner; public class Main_4 {
static int maxN = 10001;
static int maxValue = Integer.MAX_VALUE;
static ArrayList<Edge> G[] = new ArrayList[maxN];
static boolean marked[] = new boolean[maxN];
static int disto[] = new int[maxN];
static int costo[] = new int[maxN]; public static void dijkstra(int start, int n) { for (int i = 0; i <= n; i++) {
costo[i] = disto[i] = maxValue;
marked[i] = false;
} disto[start] = 0;
costo[start] = 0;
PriorityQueue<Node> pq = new PriorityQueue<Node>();
pq.add(new Node(start, 0));
marked[0] = true; Node temp;
while (!pq.isEmpty()) {
temp = pq.poll();
int v = temp.v;
if (!marked[v]) {
marked[v] = true;
int len = G[v].size();
for (int i = 0; i < len; i++) {
int target = G[v].get(i).target;
if (marked[target])
continue;
int cost = G[v].get(i).cost;
int newDist = disto[v] + cost;
if (disto[target] > newDist) {
disto[target] = newDist;
costo[target] = cost;
pq.add(new Node(target, disto[target]));
}
if (disto[target] == newDist) {
costo[target] = min(costo[target], cost);
} }
}
} int sum = 0;
for (int i = 2; i <= n; i++) {
sum += costo[i];
}
System.out.println(sum); } private static int min(int i, int j) {
// TODO Auto-generated method stub
if (i < j)
return i;
else
return j;
} public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < maxN; i++) {
G[i] = new ArrayList();
} Scanner s = new Scanner(System.in);
int numCity = s.nextInt();
int numRoad = s.nextInt();
for (int i = 0; i < numRoad; i++) {
int source = s.nextInt();
int target = s.nextInt();
int w = s.nextInt();
G[source].add(new Edge(target, w));
G[target].add(new Edge(source, w));
}
dijkstra(1, numCity);
} public static class Node implements Comparable {
public int v;
int dis; public Node(int v, int dis) {
this.v = v;
this.dis = dis;
} public int compareTo(Object o) {
Node n = (Node) o;
if (this.dis > n.dis)
return 1;
else if (this.dis == n.dis)
return 0;
else
return -1;
} } public static class Edge {
public int target, cost; public Edge(int target, int cost) {
this.target = target;
this.cost = cost;
}
}
}

ccf201604

0401折点计数(100)

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int n1=s.nextInt();
//要注意边界值的处理!!!
if(n==1) {
System.out.println(0);
return;
}
int n2=s.nextInt();
int num=0;
for(int i=0;i<n-2;i++) {
int n3=s.nextInt();
if((n2<n1&&n2<n3)||(n2>n1&&n2>n3)) {
num++;
}
n1=n2;
n2=n3;
}
System.out.println(num); } }

0404游戏(100分)(借鉴 http://blog.csdn.net/zjj582984208/article/details/55223889)

c++实现:

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner; public class Main_04 { public static class Node{
int i=0,j=0,t=0;
Node(int i1,int j1,int t1){
i=i1;
j=j1;
t=t1; }
} public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int t = s.nextInt(); int danger[][][]=new int[n][m][2];
for(int i=0;i<t;i++) {
int r=s.nextInt();
int c=s.nextInt();
int a=s.nextInt();
int b=s.nextInt();
danger[r-1][c-1][0]=a;
danger[r-1][c-1][1]=b;
}
int dir[][]= {
{1,0},
{0,1},
{-1,0},
{0,-1}
}; Queue<Node> q=new LinkedList<>();
int seen[][][]=new int[n][m][301];
q.add(new Node(0,0,0));
while(!q.isEmpty()) {
Node node=q.poll();
if(node.i==n-1&&node.j==m-1) {
System.out.println(node.t);
break;
}
for(int i=0;i<4;i++) {
int next_i=node.i+dir[i][0];
int next_j=node.j+dir[i][1];
if(next_i>=0&&next_i<n&&next_j>=0&&next_j<m&&node.t+1<300&&
(node.t+1<danger[next_i][next_j][0]||node.t+1>danger[next_i][next_j][1])
&&seen[next_i][next_j][node.t+1]==0) {
q.add(new Node(next_i,next_j,node.t+1));
seen[next_i][next_j][node.t+1]=1; }
}
} }
}

陌上花开的博客ccf

冷暖知不知ccf

WitsMakeMen的专栏算法学习

ccf的更多相关文章

  1. CCF考试

    第八次CCF考试记录 代码还不知道对不对,过两天出成绩. 成绩出来了,310分. 100+100+100+10+0: 考试13:27开始,17:30结束,提交第4题后不再答题,只是检查前四题的代码 第 ...

  2. CCF关于NOIP2014复赛报名的通知

    CCF关于NOIP2014复赛报名的通知   CCF NOIP2014复赛全部实行网上注册.报名.未通过网上报名的选手将不具备参赛和申诉资格. 系统注册须知: NOIP2014复赛注册时间:2014年 ...

  3. [CCF] Z字形扫描

    CCF Z字形扫描 感觉和LeetCode中的ZigZag还是有一些不一样的. 题目描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z ...

  4. [CCF] ISBN号码检测

    CCF ISBN号码检测 题目概述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如"x-xxx-xxxxx-x",其 ...

  5. CCF NOI系列活动

    NOI-全国青少年信息学奥林匹克竞赛全国青少年信息学奥林匹克竞赛(NOI)是国内信息学领域内面向中学生的最高水平的大赛,每省派经选拔产生的选手(其中一名是女选手)参加,NOI每年在不同的省市举行. N ...

  6. CCF考前注意几点

    1.数组最好保持初始化习惯,且检查数组初始化在循环内还是循环外,若在循环内需要对数组进行处理,则数组初始化必须放在for循环内. 2.for循环保持好习惯,用括号括起来,以免粗心. 3.if条件判断要 ...

  7. CCF真题之最优灌溉

    201412-4 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利 ...

  8. 《计算机问题求解》总结——2014年CCF计算机课程改革导教班(2014.07.11)

    一:引言     "心想事成".这是自己获得导教班学习机会的最佳概括.2013年年末学习李晓明老师的<人群与网络>课程:随后网络认识烟台大学贺利坚老师,了解到2013年 ...

  9. 2016年CCF第七次测试 俄罗斯方块

    //2016年CCF第七次测试 俄罗斯方块 // 这道小模拟题还是不错 // 思路:处理出输入矩阵中含1格子的行数和列数 // 再判是否有一个格子碰到底部,否则整体再往下移动一步,如果有一个格子不能移 ...

  10. linux 通用时钟框架CCF

    linux CCF 时钟框架 简单介绍 这里讲的时钟是给soc各组件提供时钟的树状框架,并非内核使用的时间,和其它模块一样,clk也有框架,用以适配不同的平台.适配层之上是客户代码和接口,也就是各模块 ...

随机推荐

  1. 谁再问elasticsearch集群Red怎么办?把这篇笔记给他

    前言 可能你经历过这些Red. ...等等 那ES的Red是神么意思? 这里说的red,是指es集群的状态,一共有三种,green.red.yellow.具体含义: 冷静分析 从上图可知,集群red是 ...

  2. ssh免密登陆和加密解密

    一 丶实现无密码的远程管理 1.生成公钥 私钥 [root@room9pc14 桌面]# ssh-keygen [root@room9pc14 桌面]# ls /root/.ssh/ 2.上传公钥到虚 ...

  3. docker+mysql 构建数据库的主从复制

    docker+mysql 构建数据库的主从复制 在最近的项目中,决定将项目改造成数据库读写分离的架构,后续会有博文详细讲述我的开发改造,本文主要记录我是如何一步步的构建数据库的主从复制. 为什么使用d ...

  4. 探究Dubbo的拓展机制: 上

    这篇博文是我决心深度学习Dubbo框架时记录的笔记, 主题是Dubbo的拓展点, 下面的几个部分相对来说比较零散, 貌似是不和主题挂钩的 , 并且是一些很冷门的知识点 , 但是它们确实是深入学习Dub ...

  5. 【转】c#中数组赋值方法

    C#中数组复制有多种方法,数组间的复制 ,,,};int [] alias = pins; 这里出了错误,也是错误的根源,以上代码并没有出错,但是根本不是复制,因为pins和alias都是引用,存在于 ...

  6. VMware显示错误:“未能锁定文件 无法打开磁盘 ..\*.vmdk 或者某一个快照所依赖的磁盘。”解决办法

    问题描述: 使用VMware时遇到错误:“未能锁定文件 无法打开磁盘 ..\*.vmdk 或者某一个快照所依赖的磁盘.” 问题出现的原因: 虚拟磁盘(.vmdk)本身有一个磁盘保护机制,为了防止多台虚 ...

  7. Spring HTTP invoker简介

    Spring HTTP invoker简介 Spring HTTP invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用(意味着可以通过防火墙),并使用java的序列化机制 ...

  8. Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档

    前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...

  9. sg函数的变形 - 可以将一堆石子分开

    Nim is a two-player mathematic game of strategy in which players take turns removing objects from di ...

  10. vue计算属性例子

    不使用计算属性 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...