publicclassMain{staticint[] nums =newint[11];staticboolean[] used =newboolean[13];staticlong ans =0;static{
used[7]=true;
used[11]=true;}publicstaticvoidmain(String[] args){dfs(1);System.out.println(ans /10);// 除10去除镜像和旋转的重复项}privatestaticvoiddfs(int idx){if(idx ==11){check();return;}for(int i =1; i <=12; i++){if(!used[i]){
used[i]=true;
nums[idx]= i;dfs(idx +1);
used[i]=false;}}}privatestaticvoidcheck(){int a = nums[1]+ nums[3]+ nums[6]+ nums[9];int b = nums[1]+ nums[4]+ nums[7]+ nums[10];int c = nums[2]+ nums[6]+ nums[8]+ nums[10];int d = nums[2]+ nums[3]+ nums[4]+ nums[5];int e = nums[5]+ nums[7]+ nums[8]+ nums[9];if(a == b && b == c && c == d && d == e)
ans++;}}
💖 输出
12
2. 遍历黑点
💖 源代码
publicclassMain{staticint n =4, m =5;staticchar[][] map =newchar[][]{{'.','.','.','.','#'},{'.','.','.','.','.'},{'#','@','.','.','.'},{'.','#','.','.','#'}};staticboolean[][] st =newboolean[n][m];staticboolean[][] out =newboolean[n][m];staticint[] dx ={0,1,0,-1};staticint[] dy ={1,0,-1,0};publicstaticvoidmain(String[] args){int sx =0, sy =0;// 起点坐标for(int i =0; i < n; i++)for(int j =0; j < m; j++)if(map[i][j]=='@'){
sx = i;
sy = j;}// 从起点开始搜索dfs(sx, sy);}privatestaticvoiddfs(int x,int y){if(!out[x][y]){
out[x][y]=true;System.out.println("黑点:["+ x +"]["+ y +"]");}for(int i =0; i <4; i++){int xx = x + dx[i];int yy = y + dy[i];if(xx >=0&& xx < n && yy >=0&& yy < m &&!st[xx][yy]&& map[xx][yy]=='.'){
st[x][y]=true;dfs(xx, yy);
st[xx][yy]=false;}}}}
publicclassMain{staticint ans =0;staticvoidcal(int x){while(x !=0){int t = x %10;if(t ==2)
ans++;
x /=10;}}publicstaticvoidmain(String[] args){for(int i =1; i <=2020; i++)cal(i);System.out.println(ans);}}