全部评论
我的代码:版本升级
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
publicclass
Main
{
List
<
Node
> finalList =
new
ArrayList
<>
()
;
int min =
Integer
.MAX_VALUE;
publicstaticvoid
main
(
String
[]
args
)
{
Main main =
new
Main
()
;
main.
init
()
;
}
privatevoid
init
()
{
Scanner
scanner
=
new
Scanner
(
System
.in)
;
String
[] first = scanner.
nextLine
()
.
split
(
" "
)
;
int start =
Integer
.
valueOf
(first[
0
])
;
int end =
Integer
.
valueOf
(first[
1
])
;
List
<
Node
> lists =
new
ArrayList
<>
()
;
while
(scanner.
hasNext
())
{
String
[] now = scanner.
nextLine
()
.
split
(
" "
)
;
Node node
=
new
Node
(Integer.
valueOf
(
now
[
0
])
,
Integer.
valueOf
(
now
[
1
])
,
Integer.
valueOf
(
now
[
2
]))
;
lists.
add
(node)
;
}
solve
(lists, start, end)
;
}
void
solve
(
List<Node>
lists,int
start,int
end
)
{
Collections
.sort(
lists
,
new
NodeComp())
;
List
<
Node
> tmp =
new
ArrayList
<>
()
;
Node fir =
new
Node
(
start
,
start
,
0
)
;
dfs
(fir,
lists
, tmp,
end
,
0
,
-
1
)
;
// System.out.println(finalList);
if
(finalList.
size
()
!=
0
)
{
StringBuffer sb =
print
(finalList)
;
sb.
append
(
"("
+ min +
")"
)
;
System
.
out
.println(
sb
.toString())
;
}
}
private
StringBuffer
print
(List<
Node
>
list
)
{
StringBuffer sb
=
new
StringBuffer
()
;
if
(
list
.
size
()
==
0
)
{
sb.
append
(
list
.
get
(
0
)
.begin +
"->"
+
list
.
get
(
0
)
.end)
;
}
else
{
sb.
append
(
list
.
get
(
0
)
.begin)
;
for
(
int i =
0
; i <
list
.
size
()
; i++
)
{
sb.
append
(
"->"
)
;
sb.
append
(
list
.
get
(i)
.end)
;
}
}
return sb;
}
privatevoid
dfs
(
Node
cur,
List<Node>
lists,
List<Node>
tmp,int
end,int
curCost,int
pos
)
{
if
(
cur
.end >
end
||
curCost
> min)
return;
else
if
(
cur
.end ==
end
)
{
if
(
curCost
< min)
{
min =
curCost
;
finalList =
new
ArrayList
<>
(
tmp
)
;
}
}
else
{
for
(
int i =
pos
+
1
; i <
lists
.
size
()
; i++
)
{
Node node =
lists
.
get
(i)
;
if
(node.begin ==
cur
.end)
{
tmp
.
add
(node)
;
dfs
(node,
lists
,
tmp
,
end
,
curCost
+ node.cost, i)
;
tmp
.remove(
tmp
.size()
-
1
)
;
}
else
if
(node.begin >
cur
.end)
{
break;
}
}
}
}
class
NodeComp
implements
Comparator<
Node
>
{
@Override
publicint
compare
(
Node
o1,
Node
o2
)
{
if
(
o1
.begin <
o2
.begin)
return-1;
else
if
(
o1
.begin >
o2
.begin)
return
1;
else
return
0;
}
}
class
Node
{
int begin;
int end;
int cost;
Node
(
int
begin,
int
end,
int
cost)
{
this.begin =
begin
;
this.end =
end
;
this.cost =
cost
;
}
@Override
public
String
toString()
{
return begin +
","
+ end;
}
}
}
分享
给你我的代码吧,考的时候时间差一点,考完自己再补充完成的,本地运行没什么问题,代码比较长
import java.util.Scanner;
public class WanMeiShiJie {
public static final int EMPTY = Integer.MAX_VALUE;
public static final int RED = 0;
public static final int GREEN = 1;
public static final int BLUE = 2;
public static final int YELLOW = 3;
public static final int PURPLE = 4;
public static final int[][] p = {
{RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE},
{GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE},
{BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE},
{YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED},
{YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE},
{PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED},
{YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN},
{RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN},
{GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN},
{PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}};
public static void main(String[] args) {
int numR, numG, numB, numY, numP;
numR = numG = numB = numY = numP = 0;
for(int i = 0; i < p.length; i++){
for(int j = 0; j < p[0].length; j++){
switch (p[i][j]) {
case RED:
numR++;
break;
case GREEN:
numG++;
break;
case BLUE:
numB++;
break;
case YELLOW:
numY++;
break;
case PURPLE:
numP++;
break;
default:
break;
}
}
}
System.out.println("inital :" + numR + " " + numG + " " + numB + " " + numY + " " + numP );
Scanner cin = new Scanner(System.in);
while(cin.hasNextLine()){
String str = cin.nextLine();
String[] nums = str.split(" ");
for(String num : nums){
int click = Integer.parseInt(num);
int x = click / 10;
int y = (click - 1) % 10;
int color = p[x][y];
int delete = countContinue(p, x, y);
switch (color) {
case RED:
numR -= delete;
break;
case GREEN:
numG -= delete;
break;
case BLUE:
numB -= delete;
break;
case YELLOW:
numY -= delete;
break;
case PURPLE:
numP -= delete;
break;
default:
break;
}
for(int i = 0; i < p[0].length; i++){
if(p[p.length - 1][i] == EMPTY)
moveCols(p, i);
}
}
System.out.println(numR + " " + numG + " " + numB + " " + numY + " " + numP );
}
}
public static int countContinue(int[][] p, int x, int y){
int color = p[x][y];
int start = x;
int count = 1;
while(start + 1 < p.length && p[start + 1][y] == color){
start ++;
}
int idx = start;
while(idx - 1 >= 0 && p[idx - 1][y] == color){
count ++;
idx--;
}
dispearCol(p, y, start, count);
int beforeY = y - 1;
int afterY = y + 1;
while(beforeY >= 0 && p[x][beforeY] == color){
dispearCol(p, beforeY, x, 1);
beforeY--;
count++;
}
while(afterY < p[0].length && p[x][afterY] == color){
dispearCol(p, afterY, x, 1);
afterY ++;
count++;
}
return count;
}
public static void dispearCol(int[][] p, int col, int start, int count){
int j = start;
for(int i = start - count; i >= 0; i--, j--){
p[j][col] = p[i][col];
}
for(; j >=0; j--)
p[j][col] = EMPTY;
}
public static void moveCols(int[][] p, int col){
for(int i = col, j = i + 1; j < p[0].length; i++, j++){
for(int k = 0; k < p.length; k++){
p[k][i] = p[k][j];
}
}
if(col < p[0].length - 1){
for(int k = 0; k < p.length; k++){
p[k][p[0].length - 1] = EMPTY;
}
}
}
}
分享
联想
官网直投
我也是,我投的iOS,平时做项目根本没有涉及那么多算法,楼主什么岗?
分享
var line, data = [], i = -1, s, e, arr = [], arr2 = [];
while(line = read_line()){
line = line.split(' ');
if(i == -1){
s = parseInt(line[0]);
e = parseInt(line[1]);
}
else{
data[i] = [parseInt(line[0]), parseInt(line[1]),
parseInt(line[2])];
}
i++;
}
function findNext(start, con, his){
if(start == e){
arr.push(con);
arr2.push(his + '->1050');
return con;
}
for(var n = 0, max = data.length; n < max; n++){
if(data[n][0] == start){
findNext(data[n][1], con + data[n][2], his +
'->' + data[n][0]);
}
}
}
for(var n = 0, max = data.length; n < max; n++){
if(data[n][0] == s){
findNext(data[n][1], data[n][2], '1000');
}
}
var min = arr[0], pos;
for(var n = 1; n < arr.length; n++){
if(arr[n] < min){
min = arr[n];
pos = n;
}
}
var str = arr2[pos] + '(' + min + ')';
print(str);
分享
有没有人是js写的?
分享
消消乐
import java.util.Scanner;
publicclass
Main
{
int[][]
map =newint[
10
][
10
];
publicstaticvoid
main
(
String
[]
args
)
{
Main main =
new
Main
()
;
main.
init
()
;
}
void
init
()
{
buildMap()
;
// print();
Scanner
scanner
=
new
Scanner
(
System
.in)
;
while
(scanner.
hasNext
())
{
touch(
scanner
.nextInt())
;
}
}
privatevoid
touch
(int
tar
)
{
int i, j;
if
(
tar
==
100
)
{
i =
9
;
j =
9
;
}
elseif
(
tar
==
1
)
{
i =
0
;
j =
0
;
}
else
{
j =
(
tar
-
1
)
%
10
;
i =
(
tar
-
1
)
/
10
;
}
if
(map[i][j]
!=
-
1
)
{
touch
(i, j)
;
}
zhegnli()
;
// printMap();
print()
;
}
privatevoid
printMap
()
{
for
(
int i =
0
; i <
10
; i++
)
{
StringBuffer sb
=
new
StringBuffer
()
;
for
(
int j =
0
; j <
10
; j++
)
{
sb.
append
(map[i][j]
+
" "
)
;
}
System
.
out
.println(
sb
.toString())
;
}
System
.
out
.println()
;
}
privatevoid
print
()
{
int red =
0
, green =
0
, blue =
0
, yellow =
0
, puple =
0
;
for
(
int i =
0
; i <
10
; i++
)
{
for
(
int j =
0
; j <
10
; j++
)
{
int now = map[i][j]
;
if
(now ==
0
)
red++;
else
if
(now ==
1
)
green++;
else
if
(now ==
2
)
blue++;
else
if
(now ==
3
)
yellow++;
else
if
(now ==
4
)
puple++;
}
}
System
.out.
println
(red +
" "
+ green +
" "
+ blue +
" "
+ yellow +
" "
+ puple)
;
}
privatevoid
zhegnli
()
{
for
(
int i =
0
; i <
10
;
)
{
if
(shuIsNull(
i
))
{
moveLeft(
i
)
;
}
else
{
i++;
}
}
for
(
int j =
0
; j <
10
; j++
)
{
int pos =
9
;
for
(
int i =
9
; i >=
0
; i--
)
{
if
(map[i][j]
!=
-
1
)
{
map[pos--
][j]
= map[i][j]
;
}
else
{
// System.out.println("change " + i + ","
+ pos);
}
}
for
(
int i = pos; i >=
0
; i--
)
{
map[i][j]
=
-
1
;
}
}
}
privatevoid
moveLeft
(int
pos
)
{
for
(
int i =
pos
+
1
; i <
10
; i++
)
{
for
(
int j =
0
; j <
10
; j++
)
{
map[j][i -
1
]
= map[j][i]
;
map[j][i]
=
-
1
;
}
}
}
privateboolean
shuIsNull
(int
pos
)
{
for
(
int i =
0
; i <
10
; i++
)
{
if
(map[i][
pos
]
!=
-
1
)
returnfalse;
}
returntrue;
}
privatevoid
touch
(int
i,int
j
)
{
// System.out.println("touch "+i+","+j);
deal
(
i
,
j
, map[
i
][
j
])
;
}
privatevoid
deal
(int
i,int
j,int
tar
)
{
if
(
in
(
i
,
j
)
&& map[
i
][
j
]
==
tar
)
{
map[
i
][
j
]
=
-
1
;
deal
(
i
+
1
,
j
,
tar
)
;
deal
(
i
-
1
,
j
,
tar
)
;
deal
(
i
,
j
+
1
,
tar
)
;
deal
(
i
,
j
-
1
,
tar
)
;
}
}
privateboolean
in
(int
i,int
j
)
{
if
(
i
>=
0
&&
i
<
10
&&
j
>=
0
&&
j
<
10
)
returntrue;
else
returnfalse;
}
privatevoid
buildMap
()
{
String
[]
strs
=
{
"RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE",
"GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE",
"BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE",
"YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED",
"YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE",
"PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED",
"YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN",
"RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN",
"GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN",
"PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN"
}
;
for
(
int i =
0
; i <
10
; i++
)
{
String
[] curs = strs[i]
.
split
(
","
)
;
for
(
int j =
0
; j <
10
; j++
)
{
String str = curs[j]
;
int tar =
-
1
;
if
(str.
equals
(
"RED"
))
tar =
0
;
else
if
(str.
equals
(
"GREEN"
))
tar =
1
;
else
if
(str.
equals
(
"BLUE"
))
tar =
2
;
else
if
(str.
equals
(
"YELLOW"
))
tar =
3
;
else
if
(str.
equals
(
"PURPLE"
))
tar =
4
;
map[i][j]
= tar;
}
}
}
}
分享
我升级包卡在75,你可以看看,仅供参考
分享
卧槽,好叼
分享
考试完才发现两处小错误,好亏。。。下面是修改好的,应该是对的吧→_→
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int RED = 0, GREEN = 1, BLUE = 2, YELLOW = 3, PURPLE = 4;
int[][] p = {
{RED,RED,BLUE,BLUE,GREEN,YELLOW,BLUE,YELLOW,RED,PURPLE},
{GREEN,GREEN,GREEN,BLUE,RED,PURPLE,RED,YELLOW,YELLOW,BLUE},
{BLUE,RED,RED,YELLOW,YELLOW,PURPLE,BLUE,GREEN,GREEN,BLUE},
{YELLOW,RED,BLUE,YELLOW,BLUE,RED,PURPLE,GREEN,GREEN,RED},
{YELLOW,RED,BLUE,BLUE,PURPLE,GREEN,PURPLE,RED,YELLOW,BLUE},
{PURPLE,YELLOW,RED,RED,YELLOW,RED,PURPLE,YELLOW,RED,RED},
{YELLOW,YELLOW,GREEN,PURPLE,GREEN,RED,BLUE,YELLOW,BLUE,GREEN},
{RED,YELLOW,BLUE,BLUE,YELLOW,GREEN,PURPLE,RED,BLUE,GREEN},
{GREEN,GREEN,YELLOW,YELLOW,RED,RED,PURPLE,BLUE,BLUE,GREEN},
{PURPLE,BLUE,RED,RED,PURPLE,YELLOW,BLUE,RED,RED,GREEN}};
String[] strs = sc.nextLine().split(" ");
int[] poss = new int[strs.length];
for (int i=0; i<poss.length; i++)
poss[i] = Integer.valueOf(strs[i]);
for (int i=0; i<poss.length; i++) {
int m = (poss[i] - 1) / 10;
int n = (poss[i] - 1) % 10;
int num = p[m][n];
// 如果当前位置不为空,则递归消除之
if (num == 5)
continue;
solve(p, m, n, num);
// 如果下方为空,则向下移动
for (int y=0; y<10; y++) {
int bottom = 9;
int nonempty = 9;
while (true) {
while (nonempty >= 0 && p[nonempty][y] == 5)
nonempty--;
if (nonempty < 0)
break;
if (bottom != nonempty) {
p[bottom][y] = p[nonempty][y];
p[nonempty][y] = 5;
}
bottom--;
nonempty--;
}
}
// 如果左侧整列为空,则向左移动
int left = 0;
int nonemptyline = 0;
while (true) {
while (nonemptyline < 10) {
boolean flag = true;
for (int x=0; x<10; x++) {
if (p[x][nonemptyline] != 5) {
flag = false;
break;
}
}
if (flag)
nonemptyline++;
else
break;
}
if (nonemptyline >= 10)
break;
if (left != nonemptyline) {
for (int x=0; x<10; x++) {
p[x][left] = p[x][nonemptyline];
p[x][nonemptyline] = 5;
}
}
left++;
nonemptyline++;
}
}
// 统计分类方块的数量(包含有颜色的方块和空方块)
int[] res = new int[6];
for (int x=0; x<10; x++)
for (int y=0; y<10; y++)
res[p[x][y]]++;
for (int i=0; i<5; i++) {
System.out.print(res[i]);
if (i != 4)
System.out.print(" ");
else
System.out.println();
}
}
}
private static void solve(int[][] p, int m, int n, int num) {
int[][] d = {{-1,0}, {1,0}, {0,-1}, {0,1}};
for (int i=0; i<4; i++) {
int newm = m + d[i][0];
int newn = n + d[i][1];
if (newm < 0 || newm >= 10 || newn < 0 || newn >= 10)
continue;
if (p[newm][newn] == num) {
p[newm][newn] = 5;
solve(p, newm, newn, num);
}
}
}
}
分享
相关推荐
投递4399游戏等公司10个岗位 > 2024年校招与实习信息...
点赞 评论 收藏
转发
04-10 17:50
长沙师范学院 计算机类 点赞 评论 收藏
转发
点赞 评论 收藏
转发