分号机
import java.io.IOException;
class MC{
public void run(){
int cnt = 0;
for (int i = 9; i >= 0; i--) {
for (int j = 9; j >= 0; j--) {
for (int k = 9; k >= 0; k--) {
if(i > j && j > k){
cnt ++;
}
}
}
}
//120
System.out.println(cnt);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
五星填数
全排列枚举。最后答案要除10,因为一种情况正面可以转动5次,镜像面可以转到5次,但都只是一种情况
import java.io.IOException;
class MC{
int N = 13, n = 12;
int[] a = new int[N];
boolean[] st = new boolean[N];
int res = 0;
private boolean check() {
int s1 = a[1] + a[3] + a[6] + a[9];
int s2 = a[1] + a[4] + a[7] + a[10];
int s3 = a[2] + a[3] + a[4] + a[5];
int s4 = a[2] + a[6] + a[8] + a[10];
int s5 = a[5] + a[7] + a[8] + a[9];
return s1 == s2 && s2 == s3 && s3 == s4 && s4 == s5;
}
void dfs(int cnt){
if(cnt >= 11) {
if(check()){
res++;
}
return;
}
for (int i = 1; i <= n; i++) {
if(st[i] || i == 7 || i == 11) continue;
st[i] = true;
a[cnt] = i;
dfs(cnt + 1);
st[i] = false;
}
}
public void run(){
//全排列,10个数
dfs(1);
System.out.println(res / 10);
}
}
public class Main {
public static void main(String[] args) throws IOException {
new MC().run();
}
}
显示二叉树
思路:先看看sv的是什么东西,发现是要填的数值;发现竖线下面是数字,所以是y+1,然后每个数字偏移p2个位置
class BiTree
{
private int v;
private BiTree l;
private BiTree r;
public BiTree(int v){
this.v = v;
}
public void add(BiTree the){
if(the.v < v){
if(l==null) l = the;
else l.add(the);
}
else{
if(r==null) r = the;
else r.add(the);
}
}
public int getHeight(){
int h = 2;
int hl = l==null? 0 : l.getHeight();
int hr = r==null? 0 : r.getHeight();
return h + Math.max(hl,hr);
}
public int getWidth(){
int w = (""+v).length();
if(l!=null) w += l.getWidth();
if(r!=null) w += r.getWidth();
return w;
}
public void show(){
char[][] buf = new char[getHeight()][getWidth()];
printInBuf(buf, 0, 0);
showBuf(buf);
}
private void showBuf(char[][] x){
for(int i=0; i<x.length; i++){
for(int j=0; j<x[i].length; j++)
System.out.print(x[i][j]==0? ' ':x[i][j]);
System.out.println();
}
}
private void printInBuf(char[][] buf, int x, int y){
String sv = "" + v;
int p1 = l==null? x : l.getRootPos(x);
int p2 = getRootPos(x);
int p3 = r==null? p2 : r.getRootPos(p2+sv.length());
buf[y][p2] = '|';
for(int i=p1; i<=p3; i++) buf[y+1][i]='-';
for(int i=0; i<sv.length(); i++) buf[y+1][p2+i]=sv.charAt(i); //填空位置
System.out.println();
if(p1<p2) buf[y+1][p1] = '/';
if(p3>p2) buf[y+1][p3] = '\\';
if(l!=null) l.printInBuf(buf,x,y+2);
if(r!=null) r.printInBuf(buf,p2+sv.length(),y+2);
}
private int getRootPos(int x){
return l==null? x : x + l.getWidth();
}
}
public class Main
{
public static void main(String[] args)
{
BiTree tree = new BiTree(500);
tree.add(new BiTree(200));
tree.add(new BiTree(509));
tree.add(new BiTree(100));
tree.add(new BiTree(250));
tree.add(new BiTree(507));
tree.add(new BiTree(600));
tree.add(new BiTree(650));
tree.add(new BiTree(450));
tree.add(new BiTree(510));
tree.add(new BiTree(440));
tree.add(new BiTree(220));
tree.show();
}
}
穿越雷区
穿越雷区
//记录起点和终点,bfs搜索,最先到点B即为最短
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int pro[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int dist[N][N];
int g[N][N];
PII A, B;
int main(){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ){
for (int j = 1; j <= n; j ++ ){
char c;
cin >> c;
if(c == '+') g[i][j] = 1;
else if(c == 'A') {
g[i][j] = 3;
A = {i, j};
}
else if(c == 'B') {
g[i][j] = 6;
B = {i, j};
}
else g[i][j] = 0;
}
}
memset(dist, 0x3f, sizeof dist);
dist[A.first][A.first] = 0;
queue<PII> q;
q.push(A);
while (q.size() > 0){
PII p = q.front();
q.pop();
int x = p.first, y = p.second;
//遍历邻点
for (int i = 0; i < 4; i ++ ){
int tx = x + pro[i][0], ty = y + pro[i][1];
//判断该点合不合法
if(tx < 1 || tx > n || ty < 1 || ty > n || dist[tx][ty] != 0x3f3f3f3f || g[x][y] == g[tx][ty]) continue;
dist[tx][ty] = dist[x][y] + 1;
q.push({tx, ty});
}
}
if(dist[B.first][B.second] == 0x3f3f3f3f) printf("%d", -1);
else printf("%d", dist[B.first][B.second]);
}