1110: 地砖问题

#include<bits/stdc++.h> 
using namespace std;
const double PI=acos(-1.0);
#define _max -0x7fffffff

char _map[26][26];
int visit[26][26];
int ans;
int w,h;
int x[4]={1,-1,0,0};
int y[4]={0,0,1,-1};
int p[26][26];
void dfs(int R,int C)
{

	for(int i=0;i<4;i++)
	{
		int X=R+x[i];
		int Y=C+y[i];
		if(X>=0&&X<h&&Y>=0&&Y<w&&_map[X][Y]=='.'&&visit[X][Y]==0)
		{
			visit[X][Y]=1;
			if(p[X][Y]==0)
			{
				p[X][Y]=1;
				ans++;	
			}
			dfs(X,Y);
			visit[X][Y]=0;
	
		}
		
	}
	
}
int main()
{
	while(scanf("%d%d",&w,&h)!=EOF&&(w!=0||h!=0)) //w为列,h为行 
	{
		memset(_map,'\0',sizeof(_map));
		memset(visit,0,sizeof(visit));
		memset(p,0,sizeof(p));
		ans=1;
		int R,C;//@的行和列 
		for(int i=0;i<h;i++)
			scanf("%s",_map[i]);
		for(int j=0;j<h;j++)
			for(int t=0;t<w;t++)
				if(_map[j][t]=='@')
				{
					R=j;
					C=t;
				}
		dfs(R,C); 
		printf("%d\n",ans);
	}
		
}
#include<bits/stdc++.h> 
using namespace std;
const double PI=acos(-1.0);
#define _max 0x7fffffff

char _map[30][30];
int visit[30][30]; 

int ans;
int w,h;
int _x[4]={1,-1,0,0};
int _y[4]={0,0,1,-1};

typedef struct NODE
{
int X;
int Y;		
}NODE;
queue<NODE>Q;	
void bfs(int x,int y)
{
	visit[x][y]=1;
	NODE S;
	S.X=x;
	S.Y=y;
	Q.push(S);
	while(!Q.empty())
	{
		NODE temp=Q.front();	
		Q.pop();
		for(int i=0;i<4;i++)
		{
			int xt=temp.X+_x[i];
			int yt=temp.Y+_y[i];
			if(xt>=0&&xt<h&&yt>=0&&yt<w&&visit[xt][yt]==0&&_map[xt][yt]=='.')
			{
				NODE k;
				k.X=xt;
				k.Y=yt;
				visit[xt][yt]=1;
				Q.push(k);  //Q.push({xt,xy});
				ans++;
			}
			
		}
	}	
}	




int main()
{
while(scanf("%d%d",&w,&h)!=EOF&&(w!=0||h!=0)) //w为列,h为行 
	{
		memset(_map,'\0',sizeof(_map));
		memset(visit,0,sizeof(visit));
		ans=1;
		int R,C;//@的行和列 
		for(int i=0;i<h;i++)
			scanf("%s",_map[i]);
		for(int j=0;j<h;j++)
			for(int t=0;t<w;t++)
				if(_map[j][t]=='@')
				{
					R=j;
					C=t;
				}
		bfs(R,C); 
		printf("%d\n",ans);		
	}
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一种可能的实现方式: 1. 创建一个4*4的地砖网格,每个地砖都有一个自己的编号(例如从左上角开始编号为0到15)。 2. 在场景中添加一个空物体作为游戏控制器,将以下代码挂在该物体上: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameController : MonoBehaviour { public AudioClip correctSound; public AudioClip wrongSound; public GameObject[] tiles; private int[] correctPath = {0, 1, 2, 3, 7, 11, 10, 14, 15}; private int currentStep = 0; private bool isPlayingAnimation = false; void Start() { // 初始化地砖 foreach (GameObject tile in tiles) { tile.GetComponent<Renderer>().material.color = Color.white; // 初始颜色为白色 } } void Update() { if (!isPlayingAnimation) { if (Input.GetKeyDown(KeyCode.UpArrow)) { MovePlayer(4); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { MovePlayer(-4); } else if (Input.GetKeyDown(KeyCode.LeftArrow)) { MovePlayer(-1); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { MovePlayer(1); } } } void MovePlayer(int offset) { int nextStep = currentStep + offset; if (nextStep >= 0 && nextStep <= 15) { GameObject currentTile = tiles[currentStep]; GameObject nextTile = tiles[nextStep]; if (nextStep == correctPath[currentStep]) { // 步数正确,播放正确的声音和颜色变化 AudioSource.PlayClipAtPoint(correctSound, transform.position); nextTile.GetComponent<Renderer>().material.color = Color.red; currentStep = nextStep; if (currentStep == 15) { // 路线走完,播放动画 isPlayingAnimation = true; StartCoroutine(PlayAnimation()); } } else { // 步数错误,播放错误的声音 AudioSource.PlayClipAtPoint(wrongSound, transform.position); } } } IEnumerator PlayAnimation() { Vector3[] targetPositions = new Vector3[16]; for (int i = 0; i < 16; i++) { targetPositions[i] = tiles[i].transform.position + new Vector3(0, 1, 0); // 目标位置为当前位置上方1个单位 } float duration = 1.0f; // 动画持续时间 float startTime = Time.time; while (Time.time - startTime < duration) { for (int i = 0; i < 16; i++) { Vector3 newPosition = Vector3.Lerp(tiles[i].transform.position, targetPositions[i], (Time.time - startTime) / duration); tiles[i].transform.position = newPosition; } yield return null; } // 动画结束,恢复初始位置 for (int i = 0; i < 16; i++) { tiles[i].transform.position = targetPositions[i] - new Vector3(0, 1, 0); // 恢复到初始位置下方1个单位 tiles[i].GetComponent<Renderer>().material.color = Color.white; } isPlayingAnimation = false; currentStep = 0; } } ``` 3. 在场景中添加4*4个方块作为地砖,将它们放在一个Grid布局下,并将它们的编号设置为0到15。在控制器中将这些方块赋值给tiles数组。 4. 在Assets文件夹中添加正确和错误的声音文件,并将它们赋值给控制器中的correctSound和wrongSound变量。 5. 在控制器中定义一个表示正确路线的correctPath数组,以及一个表示当前走到的位置的currentStep变量。 6. 在Update函数中检测玩家输入,如果输入了上下左右箭头,则调用MovePlayer函数移动玩家。 7. 在MovePlayer函数中检查下一步是否为正确的步数,如果是则播放正确的声音和颜色变化,并将当前步数更新为下一步。如果当前步数为15,则代表路线走完,调用PlayAnimation函数播放动画。 8. 在PlayAnimation函数中定义目标位置数组targetPositions,将所有地砖的目标位置设置为当前位置上方1个单位。然后通过Lerp函数实现移动动画,并在动画结束后将地砖恢复到初始位置下方1个单位,并将颜色恢复为白色。最后将isPlayingAnimation和currentStep变量更新为false和0。 9. 运行游戏,按照正确的路线行走,当走到最后一个地砖时播放动画。如果走错了路线,则没有声音和颜色变化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值