1.2019年 蓝桥杯A组 java省赛 试题 I: 糖果
import java. util. Scanner;
public class _9 糖果 {
static int min= ( int ) 1e+100 ;
static int suger[ ] [ ] = new int [ 102 ] [ 4 ] ;
static int n, m, k;
static int visitsuger[ ] = new int [ 102 ] ;
static boolean visit[ ] = new boolean [ 102 ] ;
public static void main ( String[ ] args) {
Scanner in = new Scanner ( System. in) ;
n= in. nextInt ( ) ;
m= in. nextInt ( ) ;
k= in. nextInt ( ) ;
for ( int i= 0 ; i< n; i++ )
{
for ( int j= 0 ; j< k; j++ )
{
suger[ i] [ j] = in. nextInt ( ) ;
}
}
dfs ( 0 , 0 ) ;
System. out. println ( min) ;
}
static boolean check ( )
{
for ( int i= 1 ; i<= m; i++ )
{
if ( visitsuger[ i] == 0 )
return false ;
}
return true ;
}
static void dfs ( int i, int res)
{
if ( i>= n)
{
if ( check ( ) )
{
if ( res< min)
min= res;
}
return ;
}
visit[ i] = true ;
for ( int j= 0 ; j< k; j++ )
{
visitsuger[ suger[ i] [ j] ] ++ ;
}
dfs ( i+ 1 , res+ 1 ) ;
visit[ i] = false ;
for ( int j= 0 ; j< k; j++ )
{
visitsuger[ suger[ i] [ j] ] -- ;
}
dfs ( i+ 1 , res) ;
}
}
2.蓝桥杯 全球变暖
package dfs续;
import java. util. Scanner;
public class _1 全球变暖 {
static int n;
static boolean visit[ ] [ ] = new boolean [ 100 ] [ 100 ] ;
static char mapp[ ] [ ] = new char [ 100 ] [ 100 ] ;
static int insland [ ] = new int [ 100001 ] ;
static void dfs ( int i, int j, int k )
{
if ( i< 1 || j< 1 || i> n- 2 || j> n- 2 )
{
return ;
}
if ( mapp[ i] [ j] == '.' )
return ;
if ( visit[ i] [ j] == true )
return ;
visit[ i] [ j] = true ;
if ( mapp[ i- 1 ] [ j] == '#' && mapp[ i+ 1 ] [ j] == '#' && mapp[ i] [ j- 1 ] == '#' && mapp[ i] [ j+ 1 ] == '#' && mapp[ i] [ j] == '#' )
{
insland[ k] ++ ;
}
dfs ( i- 1 , j, k) ;
dfs ( i+ 1 , j, k) ;
dfs ( i, j- 1 , k) ;
dfs ( i, j+ 1 , k) ;
}
public static void main ( String[ ] args) {
Scanner in= new Scanner ( System. in) ;
int k= 0 , res= 0 ;
n = in. nextInt ( ) ;
String temp= in. nextLine ( ) ;
for ( int i = 0 ; i < n; i++ ) {
String line = in. nextLine ( ) ;
mapp[ i] = line. toCharArray ( ) ;
}
for ( int i= 1 ; i<= n- 2 ; i++ )
{
for ( int j= 1 ; j<= n- 2 ; j++ )
{
if ( mapp[ i] [ j] == '#' && visit[ i] [ j] == false )
{
k++ ;
dfs ( i, j, k) ;
}
}
}
for ( int i= 1 ; i<= k; i++ )
{
if ( insland[ i] == 0 )
res++ ;
}
System. out. print ( res) ;
}
}
3.方格填数 蓝桥杯
package dfs续;
public class _2 方格填数 {
public static int count;
public static int [ ] [ ] map = new int [ 3 ] [ 4 ] ;
public static int [ ] visited = new int [ 10 ] ;
public static int m = map. length;
public static int n = map[ 0 ] . length;
public static int dir[ ] [ ] = new int [ ] [ ] {
{ 0 , - 1 } ,
{ - 1 , - 1 } ,
{ - 1 , 0 } ,
{ - 1 , 1 }
} ;
public static boolean checked ( int x, int y, int num) {
for ( int i = 0 ; i < 4 ; i++ ) {
int nx = x + dir[ i] [ 0 ] ;
int ny = y + dir[ i] [ 1 ] ;
if ( nx >= 0 && nx < 3 && ny >= 0 && ny < 4 ) {
if ( map[ nx] [ ny] == num - 1 || map[ nx] [ ny] == num + 1 ) {
return false ;
}
}
}
return true ;
}
public static void dfs ( int x, int y) {
if ( x == m- 1 && y == n- 1 ) {
count++ ;
return ;
}
if ( y >= n) {
dfs ( x + 1 , 0 ) ;
} else {
for ( int i= 0 ; i<= 9 ; i++ ) {
if ( visited[ i] == 0 && checked ( x, y, i) ) {
visited[ i] = 1 ;
map[ x] [ y] = i;
dfs ( x, y+ 1 ) ;
map[ x] [ y] = 100 ;
visited[ i] = 0 ;
}
}
}
}
public static void main ( String[ ] args) {
for ( int i= 0 ; i< m; i++ ) {
for ( int j= 0 ; j< n; j++ ) {
map[ i] [ j] = 100 ;
}
}
dfs ( 0 , 1 ) ;
System. out. println ( count) ;
}
}
4.牌型总数
package dfs续;
public class _3 牌型种数 {
public static void main ( String[ ] args) {
dfs ( 1 , 0 ) ;
System. out. println ( res) ;
}
static int a[ ] = new int [ 14 ] ;
static int res= 0 ;
static void dfs ( int n, int cnt)
{
if ( n== 14 )
{
if ( cnt== 13 )
res++ ;
return ;
}
if ( n> 13 )
return ;
for ( int i= 0 ; i<= 4 ; i++ )
{
dfs ( n+ 1 , cnt+ i) ;
}
}
}