码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1753解题报告以及实现

时间:2015-12-17 20:59:44      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

题目大意就是:4*4的矩形盘,有黑白两种状态的矩形,点击一个矩形,那么它以及它周围的4个方向的矩形都会变为相反的状态。技术分享

当然显而易见,步数最多不过16步,因为把一个位置点击两下就会变成原来的状态。所以就可以枚举这16个状态。再用dfs就很容易的AC了。

 

做完这个题,一时手痒,正好最近在学java就把这个游戏写出来了,不过还没学到Swing,所以写的有点乱。凑合着看看吧。技术分享

  1 import java.awt.*;
  2 import java.awt.event.*;
  3 import javax.swing.*;
  4 import java.util.Random;
  5 public class Game1
  6 {
  7     public static void main(String[] args)
  8     {
  9         JFrame mf = new MyFrame("Game");
 10     }
 11 }
 12 class MyFrame extends JFrame
 13 {    
 14     public JButton[][] but = new JButton[4][4]; //创建4*4的按钮
 15     public Container contentPane = getContentPane();  //得到容器
 16     public MyFrame(String title)
 17     {
 18         super(title);
 19         contentPane.setBackground(Color.white);
 20         contentPane.setLayout(new GridLayout(4,4)); //设置布局管理器
 21         //添加菜单
 22         MenuBar mb = new MenuBar();
 23         Menu menu = new Menu("菜单");
 24         MenuItem newGame = new MenuItem("新游戏");
 25         MenuItem showWay = new MenuItem("开挂");
 26         
 27         ActionListener actnewGame = new  actionNew();
 28         newGame.addActionListener(actnewGame);
 29         ActionListener actshowWay = new actionShow();
 30         showWay.addActionListener(actshowWay);
 31         
 32         menu.add(newGame);
 33         menu.add(showWay);
 34         mb.add(menu);
 35         this.setMenuBar(mb);
 36         
 37         MyActionListener act = new MyActionListener();
 38         
 39         for(int i=0;i<4;i++)    //设置按钮属性并加入到容器中
 40         for(int j=0;j<4;j++)
 41         {
 42             but[i][j] = new JButton();
 43             Random r = new Random();
 44             if(r.nextInt() % 2==0) but[i][j].setBackground(Color.black);
 45             else but[i][j].setBackground(Color.white);
 46             contentPane.add(but[i][j]);
 47             but[i][j].addActionListener(act); //为按钮设置事件监听器
 48             but[i][j].setActionCommand(""+i+j);
 49         }
 50         
 51         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 52         this.setSize(350,260);
 53         this.setVisible(true);
 54     }
 55     class actionNew implements ActionListener //按钮事件监听器
 56     {
 57         public void actionPerformed(ActionEvent e)
 58         {
 59             for(int i=0;i<4;i++)   
 60              for(int j=0;j<4;j++)
 61              {
 62                 Random r = new Random();
 63                 if(r.nextInt() % 2==0) but[i][j].setBackground(Color.black);
 64                 else but[i][j].setBackground(Color.white);
 65              }
 66         }
 67     }
 68     class actionShow implements ActionListener //开挂菜单监听器
 69     {
 70         private int step;
 71         private data[] dataWay = new data[17];
 72         private boolean flag;
 73         private MyActionListener mac = new MyActionListener();
 74         
 75         public void actionPerformed(ActionEvent e)
 76         {
 77              flag=false;
 78              for(step=0;step<=16;step++)
 79              {
 80                  dfs(0,0,0);
 81                  if(flag) break;
 82              }
 83              System.out.println("--------------------");
 84              if(flag)
 85              {
 86                 for(int i=0;i<step;i++)
 87                     System.out.println(dataWay[i].x+" "+dataWay[i].y);
 88              }
 89              else System.out.println("Impossible!");
 90         }
 91         class data 
 92         {
 93             public int x,y;
 94         }
 95         public void dfs(int i,int j,int deep) //深度优先搜索寻找答案
 96         {
 97             if(deep==step)
 98             {
 99                 flag=mac.isOver();
100                 return;
101             }
102             if((flag)||(i==4)) return;
103             
104             dataWay[deep] = new data(); //记录走法
105             dataWay[deep].x = i+1;
106             dataWay[deep].y = j+1;
107             
108             if((i*10+j<34)&&(i*10+j>=0)) mac.change(i*10+j);
109             if(j<3) dfs(i,j+1,deep+1);
110             else dfs(i+1,0,deep+1);
111     
112             if((i*10+j<34)&&(i*10+j>=0)) mac.change(i*10+j);
113             if(j<3) dfs(i,j+1,deep);
114             else dfs(i+1,0,deep);
115             return;
116         }
117     }
118     class MyActionListener implements ActionListener  //设置按钮监听器
119     {
120         public final int[] r={0,0,0,1,-1};
121         public final int[] l={0,1,-1,0,0};
122     
123         public void actionPerformed(ActionEvent e)
124         {
125             change(Integer.parseInt(e.getActionCommand())); 
126             if(isOver()) 
127               JOptionPane.showMessageDialog(null, "恭喜过关,智商完全压制!", "提示", JOptionPane.INFORMATION_MESSAGE);
128             
129         }
130         public void change(int n)//改变按钮的颜色
131         {
132             int i,j;
133             i=n/10;
134             j=n%10;
135             for(int p=0;p<5;p++)
136             {
137                 int u,k;
138                 u=i;k=j;
139                 if((i+r[p]>=0)&&(i+r[p]<4)) u=i+r[p];
140                 if((j+l[p]>=0)&&(j+l[p]<4)) k=j+l[p];
141                 if(null!=but[u][k]) 
142                 {
143                     if(Color.black==but[u][k].getBackground())
144                         but[u][k].setBackground(Color.white);
145                     else
146                         but[u][k].setBackground(Color.black);
147                 }
148             }
149         }
150         public boolean isOver()//判断游戏是否结束
151         {
152             for(int i=0;i<4;i++)
153                 for(int j=0;j<4;j++)
154                 {
155                     if(but[i][j].getBackground()!= but[0][0].getBackground()) 
156                         return false;
157                 }
158             return true;
159         }
160     }
161 }

 

POJ1753解题报告以及实现

标签:

原文地址:http://www.cnblogs.com/La-mer/p/5055219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!