1.20
1 java快读快写记录
import java.io.*;
public class test {
public static void main(String args[]) throws IOException{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
st.nextToken();
String str = st.sval;//读取String类型数据
st.nextToken();
double num1 = st.nval;//读取double类型数据
st.nextToken();
int num2 = (int)st.nval;//读取int类型数据
st.nextToken();
long num3 = (long)st.nval;//读取long类型数据
}
}
import java.io.*;
public class test {
public static void main(String args[]){
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
pw.print();//不换行输出
pw.println();//换行输出
pw.printf();//格式化输出
pw.flush();//关闭输出流
}
}
import java.io.*;
public class test {
public static void main(String args[]) throws IOException{
// StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
String x = re.readLine();
System.out.println(x);
}
}
2
UCV2013H - Slick - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 分块记录次数
- java要爆栈,但是理论上是对的
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
using namespace std;
const int N = 255;
int g[N][N];
bool str[N][N];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int n, m, cnt;
map<int,int>myMap;
void solve(int x, int y) {
for(int i = 1; i <= x; i++) {
for(int j = 1; j <= y; j++) {
cin>>g[i][j];
}
}
}
void dfs(int x, int y) {
for(int i = 0; i < 4; i++) {
int nowx = x + dx[i];
int nowy = y + dy[i];
if(nowx >= 1 && nowx <= n && nowy <= m && nowy >= 1) {
if(str[nowx][nowy] == false && g[nowx][nowy] == 1) {
str[nowx][nowy] = true;
cnt++;
dfs(nowx, nowy);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>n>>m;
while(n != 0 && m != 0) {
solve(n, m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(str[i][j] == false && g[i][j] == 1) {
cnt = 1;
str[i][j] = true;
dfs(i, j);
if(myMap[cnt] == 0) {
myMap[cnt] = 1;
} else {
myMap[cnt]++;
}
}
}
}
int cnt_ = 0;
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
cnt_ += it->second;
}
cout<<cnt_<<endl;
for (auto it = myMap.begin(); it != myMap.end(); ++it) {
std::cout << it->first << " " << it->second << std::endl;
}
cin>>n>>m;
memset(g, 0, sizeof(g));
memset(str, false, sizeof(str));
myMap.clear();
}
return 0;
}
package shuati;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class Main {
static final int N = 255;
static int[][] g = new int[N][N];
static boolean[][] str = new boolean[N][N];
static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int dx[] = {1, 0, -1, 0};
static int dy[] = {0, 1, 0, -1};
static int n, m, cnt;
static Map<Integer, Integer>myMap = new HashMap<>();
static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
public static void solve(int x, int y) throws IOException{
for(int i = 1; i <= x; i++) {
for(int j = 1; j <= y; j++) {
cin.nextToken();
g[i][j] = (int)cin.nval;
}
}
}
public static void dfs(int x, int y) {
for(int i = 0; i < 4; i++) {
int nowx = x + dx[i];
int nowy = y + dy[i];
if(nowx >= 1 && nowx <= n && nowy <= m && nowy >= 1) {
if(str[nowx][nowy] == false && g[nowx][nowy] == 1) {
str[nowx][nowy] = true;
cnt++;
dfs(nowx, nowy);
}
}
}
}
public static void main(String[] args) throws IOException{
//BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
//Scanner scanner = new Scanner(System.in);
cin.nextToken();
n = (int)cin.nval;
cin.nextToken();
m = (int)cin.nval;
while(n != 0 && m != 0) {
solve(n, m);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(str[i][j] == false && g[i][j] == 1) {
cnt = 1;
str[i][j] = true;
dfs(i, j);
if(myMap.get(cnt) == null) {
myMap.put(cnt, 1);
} else {
myMap.put(cnt, myMap.get(cnt) + 1);
}
}
}
}
int cnt_ = 0;
for(Map.Entry<Integer, Integer> entry : myMap.entrySet()) {
cnt_ += entry.getValue();
}
cout.println(cnt_);
for(Map.Entry<Integer, Integer> entry : myMap.entrySet()) {
cout.println(entry.getKey() + " " + entry.getValue());
}
cin.nextToken();
n = (int)cin.nval;
cin.nextToken();
m = (int)cin.nval;
Arrays.fill(g, 0);
Arrays.fill(str, false);
myMap.clear();
}
return;
}
}
[ARC001B] リモコン - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 此题就别乱爆搜了,可以想象比他大的可以往小调,比他小的往大的调,就把幂从6 -> 3了,然后,次数大的返回,剪一下枝
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
static final int N = (int)1e2 + 10;
static int ans = (int)1e5;
static boolean str;
public static void dfs(int x, int y, int cnt) {
if(x == y) {
ans = Math.min(ans, cnt);
return;
}
if(cnt >= ans) return;
if(x < y) {
dfs(x + 10, y, cnt + 1);
dfs(x + 5, y, cnt + 1);
dfs(x + 1, y, cnt + 1);
}else {
dfs(x - 10, y, cnt + 1);
dfs(x - 5, y, cnt + 1);
dfs(x - 1, y, cnt + 1);
}
}
public static void main(String[] args) throws IOException{
StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));'
cin.nextToken();
int a = (int)cin.nval;
cin.nextToken();
int b = (int)cin.nval;
dfs(a, b, 0);
System.out.println(ans);
return;
}
}
Forgery - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 感觉根根本不是搜索,直接一个目标数组和操作数组,比对就行了,反正都只有1e6,根本不可能吃T
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main {
static final int N = (int)1e3 + 10;
static boolean[][] target = new boolean[N][N];
static boolean[][] now = new boolean[N][N];
public static void solve(int x, int y) {
if(target[x + 1][y + 1] == true && target[x + 1][y] == true && target[x - 1][y] == true && target[x][y + 1] == true) {
if(target[x][y - 1] == true && target[x + 1][y - 1] == true && target[x - 1][y - 1] == true && target[x - 1][y + 1] == true) {
now[x + 1][y + 1] = true;
now[x + 1][y] = true;
now[x - 1][y] = true;
now[x][y + 1] = true;
now[x][y - 1] = true;
now[x + 1][y - 1] = true;
now[x - 1][y - 1] = true;
now[x - 1][y + 1] = true;
}
}
}
public static void dfs(int x, int y, int cnt) {
}
public static void main(String[] args) throws IOException{
//StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
//PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));'
Scanner scanner = new Scanner(System.in);
// cin.nextToken();
// int x = (int)cin.nval;
// cin.nextToken();
// int y = (int)cin.nval;
int x = scanner.nextInt();
int y = scanner.nextInt();
for (int i = 1; i <= x; i++) {
//cin.nextToken();
String s;
s = scanner.next();
for(int j = 1; j <= y; j++) {
if(s.charAt(j - 1) == '#') {
target[i][j] = true;
}
}
}
for(int i = 2; i <= x - 1; i++) {
for(int j = 2; j <= y - 1; j++) {
solve(i, j);
}
}
for(int i = 1; i <= x; i++) {
for(int j = 1; j <= y; j++) {
if(now[i][j] != target[i][j]) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
return;
}
}
浙公网安备 33010602011771号