我正在尝试回答CodeForces上的1A剧院广场问题。 它一直告诉我:"测试1"运行时错误
这是我的代码:
Animation dwn,up, left, right;
BufferedImage north, south, east, west;
Sprites sprites = new Sprites();
KeyManager keyManager = new KeyManager();
Rectangle boundsPlayer;
//Controller c;
boolean movUp, movDwn, movRight, movLeft;
String direction = "";
public Player(int x, int y){
super(x,y);
sprites.create();
dwn = new Animation(100, sprites.player_down);
up = new Animation(100, sprites.player_up);
left = new Animation(100, sprites.player_left);
right = new Animation(100, sprites.player_right);
north = sprites.playerNorth;
south = sprites.playerSouth;
east = sprites.playerEast;
west = sprites.playerWest;
boundsPlayer = new Rectangle(0,0, 32, 32);
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public Rectangle getBounds(){
return new Rectangle((int) x, (int) y, 32, 32);
}
/**
* player tick method, used to move the player
*/
public void update(){
if(Game.getKeyManager().up){
y -= 3;
direction = "north";
}
if(Game.getKeyManager().down){
y += 3;
direction = "south";
}
if(Game.getKeyManager().left){
x -= 3;
direction = "west";
}
if(Game.getKeyManager().right){
x += 3;
direction = "east";
//System.out.println(x);
}
}
public int getX() {
return (int)x;
}
public int getY() {
return (int)y;
}
public String getDirection(){
return direction;
}
/**
* method used to return players facing direction sprite
* @return
*/
public BufferedImage getPlayerDirection(){
if(direction == "north"){
return north;
}
else if(direction == "south"){
return south;
}
else if(direction == "east"){
return east;
}
else{
return west;
}
}
public void render(Graphics g){
g.drawImage(getCurrentAnimationFrame(), (int) x, (int) y, null);
//g.setColor(Color.red);
//g.fillRect((int) x + boundsPlayer.x, (int) y + boundsPlayer.y, boundsPlayer.width, boundsPlayer.height);
}
/**
* method used to return the bufferedImage of current frame
* @return
*/
public BufferedImage getCurrentAnimationFrame(){
if(Game.getKeyManager().right == true){
return right.getCurrentFrame();
}
else if(Game.getKeyManager().left == true){
return left.getCurrentFrame();
}
else if(Game.getKeyManager().up == true){
return up.getCurrentFrame();
}
else if(Game.getKeyManager().down == true){
return dwn.getCurrentFrame();
}
else {
return getPlayerDirection();
}
}
我还是初学者,所以如果您有任何意见或建议,我们将非常感谢并感谢您抽出时间阅读我的问题。
答案 0 :(得分:0)
您将获得超出时间限制,因为您使用的概念有2个循环,即O(n)的时间复杂度。但实际的解决方案将具有O(1)的时间复杂度,即它不需要任何循环。
提示: - 您将需要,只需几个基于其他的解决方案。