团体程序设计天梯赛-练习集——7-8 估值一亿的AI核心代码

5 篇文章 1 订阅

7-8 估值一亿的AI核心代码

在这里插入图片描述

以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

无论用户说什么,首先把对方说的话在一行中原样打印出来;
消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
把原文中所有大写英文字母变成小写,除了 I;
把原文中所有独立的 can you、could you 对应地换成 I can、I could—— 这里“独立”是指被空格或标点符号分隔开的单词;
把原文中所有独立的 I 和 me 换成 you;
把原文中所有的问号 ? 换成惊叹号 !;
在一行中输出替换后的句子作为 AI 的回答。

输入格式:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出格式:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例:

6
Hello ?
Good to chat with you
can you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
Good to chat with you
AI: good to chat with you
can you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don’t know

正确代码(java):

public class Main {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Scanner sc=new Scanner(System.in);
		String str;
		int n;
		n=Integer.valueOf(sc.nextLine());
		while(n!=0){
			str=sc.nextLine();
			System.out.println(str);
			//str=str.toLowerCase();
			str=str.trim();
			str=str.replaceAll("\\s+", " ");
			char arr[]=str.toCharArray();
			int len=arr.length;
			str="";
			for(int i=0;i<len;i++){
				if(arr[i]>='A'&&arr[i]<='Z'&&arr[i]!='I'){
					arr[i]+=32;
				}
				str+=arr[i];
			}
			str=str.replace("吗", "");
			str=str.replaceAll("\\?", "!");
			str=str.replaceAll(" (\\W)", "$1");//去掉标点符号前面的空格,$1:表示第一个小括号里的内容
			str=str.replaceAll("\\bcan you\\b", "A");//\\b:边界符
			str=str.replaceAll("\\bcould you\\b", "B");
			str=str.replaceAll("\\b(me|I)\\b", "C");
			str=str.replaceAll("A", "I can");//用A,B,C代替要更改的字符串以免“I”再次进行转换
			str=str.replaceAll("B", "I could");
			str=str.replaceAll("C", "you");
			System.out.println("AI: "+str);
			n--;
		}
	}

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是代码实现: ```python # 定义下棋的函数 def play_chess(board, player, move): row, col = move board[row][col] = player return board # 定义函数 def evaluate_board(board): # 这里只是简单的,可根据实际情况进行调整 player_one = 0 player_two = 0 for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == 1: player_one += 1 elif board[i][j] == 2: player_two += 1 return player_one - player_two # 定义alpha-beta剪枝算法 def alpha_beta_pruning(board, player, depth, alpha, beta): # 判断是否到达搜索深度或终局,到达则返回当前局面的 if depth == 0 or is_game_over(board): return evaluate_board(board) # 判断当前局面下哪些位置可以落子 moves = get_possible_moves(board, player) # 将可行落子位置按照从高到低排序 moves = sorted(moves, key=lambda x: evaluate_board(play_chess(board, player, x)), reverse=True) # 如果当前玩家是自己,则进行max节点的搜索 if player == MY_PLAYER: for move in moves: new_board = play_chess(board, player, move) alpha = max(alpha, alpha_beta_pruning(new_board, get_opponent(player), depth-1, alpha, beta)) if beta <= alpha: break return alpha # 如果当前玩家是对手,则进行min节点的搜索 else: for move in moves: new_board = play_chess(board, player, move) beta = min(beta, alpha_beta_pruning(new_board, get_opponent(player), depth-1, alpha, beta)) if beta <= alpha: break return beta # 定义获取对手的函数 def get_opponent(player): if player == 1: return 2 else: return 1 # 定义获取可行落子位置的函数 def get_possible_moves(board, player): moves = [] for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == 0: if is_valid_move(board, player, (i, j)): moves.append((i, j)) return moves # 定义判断是否可以落子的函数 def is_valid_move(board, player, move): row, col = move # 判断该位置是否为空 if board[row][col] != 0: return False # 判断左、右、上、下、左上、右下、右上、左下八个方向是否可以翻子 directions = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1), (1, -1), (-1, 1)] result = False for direction in directions: r, c = row + direction[0], col + direction[1] temp = False while r >= 0 and r < len(board) and c >= 0 and c < len(board[0]) and board[r][c] == get_opponent(player): r += direction[0] c += direction[1] temp = True if r >= 0 and r < len(board) and c >= 0 and c < len(board[0]) and board[r][c] == player and temp: result = True return result # 定义判断游戏是否结束的函数 def is_game_over(board): for i in range(len(board)): for j in range(len(board[0])): if board[i][j] == 0: return False return True # 定义下棋的函数 def make_move(board, player, depth): # 判断当前玩家可以落子的位置 moves = get_possible_moves(board, player) if len(moves) == 0: return None # 否则进行搜索 moves = sorted(moves, key=lambda x: alpha_beta_pruning(play_chess(board, player, x), get_opponent(player), depth-1, -1000000, 1000000), reverse=True) return moves[0] ``` 这是一个比较简单的实现,如果要进一步优化,可以考虑加入置换表、启发式搜索等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值